You are not logged in.

#1 2012-10-01 16:28:36

Jayfree
Member
Registered: 2012-09-22
Posts: 5

sudo command not executing in startup shell script

Hello All,

Summary:
I have written a shell script to synchronize time from Internet during startup. The command to synchronize time " ntpd -qg " needs sudo. But it is not executing during startup. I think it is a file permission issue. Could anyone of you help me ?

Detailed Description:
Each time when my computer boots up, I want to synchronize my time from Internet. For that I have written a shell script and in order to execute in startup, I have made the changes according to https://wiki.archlinux.org/index.php/Sy … process.3F

The script is simple. Below is the script which I have written

#!/bin/sh

cd /home/user/            #inorder to store the files old_time and updated_time in home folder
date > old_time          #store the current time into file old_time
sudo ntpd -qg             #command for synchronising the time
date > updated_time #store the updated time into file updated_time

When I reboot my computer, I can see the files old_time and updated_time. So the startup script is running. But the command "sudo ntpd -qg" is not getting executed. Actually my computer doesn't have hardware clock(It is an Raspberry Pi). So each time when it boots, the year is 1970 but after update it gets corrected to 2012.  I can see that the year in both the files are same (so "sudo ntpd -qg" is not getting executed ).

If I run the script using " ./script.sh " , it is executing and the time gets updated. For file permission I had executed "chmod +x script.sh". Also I have configured my system in such a manner that sudo never asks for a password.

I think this is a file permission issue. Is there any ways to fix this ? How can I get the sudo command executed ?
It is not an issue with typing passwords since sudo command in my machine doesnot need a password.

Thanks and Best Regards,
Jeffrey Antony

Last edited by Jayfree (2012-10-01 16:42:10)

Offline

#2 2012-10-01 22:09:41

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,024

Re: sudo command not executing in startup shell script

Some thoughts:
If systemd is running the script during boot there should be no need to use sudo.
Is your /home mounted before the script runs?
The wiki suggests moving a script to /usr/bin, why run it from /home/you?


You're just jealous because the voices only talk to me.

Offline

#3 2012-10-01 22:36:05

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: sudo command not executing in startup shell script

Yeah, how exactly are you running it?  Are you using SysV or systemd?  In either case, during the boot process, things are run as root in general.  So if you put it in your rc.conf for example, it would not need sudo, and if you made a service in systemd, it would also not need sudo.  You could even make a rc.d file for it, and that too would not need sudo.

Offline

#4 2012-10-02 05:47:59

Jayfree
Member
Registered: 2012-09-22
Posts: 5

Re: sudo command not executing in startup shell script

@moetunes: I tried placing the script in /usr/bin folder. But no use. The time is not updated.

@WonderWoofy: I am running my script using systemd. Below is my script

[Unit]
Description=rpi_user 

[Service]
ExecStart=/usr/bin/rpi_startup.sh

[Install]
WantedBy=multi-user.target

Any other suggestion ?

I cannot understand why the time is not updating. The internet is connected because above the time update command "ntpd -qg" , if I put an wget for a file, that file gets downloaded.

Regards,
Jeffrey

Offline

#5 2012-10-02 05:55:58

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,412

Re: sudo command not executing in startup shell script

Why are you using a script to issue three commands?  Why not just write those into the service file?  Also, why not just use the ntpd.service, doesn't it already update pretty early? 

If you are set on sticking with this plan though, I think you at least need to have the "After=network.target" in the [Unit] section.

Offline

#6 2012-10-02 06:25:40

2ManyDogs
Member
Registered: 2012-01-15
Posts: 1,706

Re: sudo command not executing in startup shell script

Doesn't the raspberry pi run Arch Arm? I think you should really be asking about this on their forum.

Offline

#7 2012-10-02 06:34:54

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 19,356
Website

Re: sudo command not executing in startup shell script

2ManyDogs wrote:

Doesn't the raspberry pi run Arch Arm? I think you should really be asking about this on their forum.


Indeed. Please see the ARM boards for support for the Pi.


Closing


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB