jal
Posts: 28
Joined: Mon Oct 01, 2012 10:47 am
Location: Estonia

Best way to start shell script on startup?

Fri Oct 26, 2012 8:56 am

Hi all,

What would be best way to run shell script on startup?

User avatar
joan
Posts: 15378
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 11:09 am

I prefer to make a reboot entry in crontab. Others prefer adding entries in /etc/init.d.

For crontab use

sudo crontab -e

to create and

sudo crontab - l

to list.

For details see

man crontab

man 5 crontab

jal
Posts: 28
Joined: Mon Oct 01, 2012 10:47 am
Location: Estonia

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 1:11 pm

If I want to run my script once in every minute?

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 1:16 pm

@jal: Then the answer is definitely ``crontab``.

Code: Select all

while not self.asleep():
    sheep += 1

jal
Posts: 28
Joined: Mon Oct 01, 2012 10:47 am
Location: Estonia

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 1:37 pm

should i add something like this:

* * * * * ./myscript.sh

?

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 1:43 pm

/etc/rc.local

is for misc startup stuff. But it does sound like you might want to crontab a script
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 1:57 pm

Note that if you really want it to run every minute, it will probably be easier to include that logic in the script itself.

I.e.,:

while :;do
# do stuff
sleep 60
done

Then you just need some way to launch it on startup and you're all set.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

jal
Posts: 28
Joined: Mon Oct 01, 2012 10:47 am
Location: Estonia

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 2:02 pm

Yes, when tested this script I used "sleep 60"

I dont't really care if this timing is done by cron or script, I just want it to work. I have added quite few different lines to crom, but nothing helps :(

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 2:06 pm

One reason why it is better to do it in the script (one of many) is that it avoids cron logfile clutter.

If you have something spawning an entry in the cron log every minute, you find yourself unable to effectively read that log file.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
joan
Posts: 15378
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 2:09 pm

Add the following line to yours or the root crontab. It's probably simplest to add it to root so

sudo crontab -e

Add the line

* * * * * /home/<your-directory>/<your-script>

Use

sudo crontab -l

to verify the line was added correctly.

Ensure your script is executable

chmod +x /home/<your-directory>/<your-script>

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Best way to start shell script on startup?

Fri Oct 26, 2012 2:14 pm

Bad advice, as noted above.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “General programming discussion”