Running bash script as a service?


11 posts
by gmc » Sat Aug 17, 2013 8:01 pm
I've got a simple bash script that runs and every minute it posts data to the internet, so the script runs in a loop.

What is the easiest way to run this as a service so the script auto starts when the pi boots up?
Posts: 93
Joined: Fri Mar 09, 2012 11:31 am
by joan » Sat Aug 17, 2013 8:04 pm
One way is to add a cron entry using the @reboot time.

man crontab

man 5 crontab
User avatar
Posts: 5373
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by kghunt » Sat Aug 17, 2013 8:12 pm
A more stable way is to install upstart and create an upstart service (easier than it sounds).
Posts: 350
Joined: Sun Mar 04, 2012 9:28 am
by gmc » Sat Aug 17, 2013 8:28 pm
Thanks - upstart did the trick. That was easy :)

This also helped me out.:
viewtopic.php?t=49153&p=383782
Posts: 93
Joined: Fri Mar 09, 2012 11:31 am
by kghunt » Sat Aug 17, 2013 8:38 pm
Ahh that was me too :-) I use upstart all the time.
Posts: 350
Joined: Sun Mar 04, 2012 9:28 am
by pluggy » Sun Aug 18, 2013 12:37 pm
I'd just make it not a loop, and run it every minute from crontab. It starts automatically every minute....
Don't judge Linux by the Pi.......
User avatar
Posts: 2402
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by Joe Schmoe » Sun Aug 18, 2013 12:42 pm
pluggy wrote:I'd just make it not a loop, and run it every minute from crontab. It starts automatically every minute....


Bad idea.

As I've argued many times in the past, it is a very bad idea to run a job (any job) every minute via cron.

Much better to have the job control itself (via looping).
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2589
Joined: Sun Jan 15, 2012 1:11 pm
by pluggy » Sun Aug 18, 2013 12:51 pm
You think everything is a bad idea. And don't often back up your arguments. You were spitting feathers about minicom the last time our paths crossed.

Provided the script in question is finished well inside the minute, its more than happy. It only gets messy if the script overruns.
Don't judge Linux by the Pi.......
User avatar
Posts: 2402
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by Joe Schmoe » Sun Aug 18, 2013 2:59 pm
You think everything is a bad idea. And don't often back up your arguments. You were spitting feathers about minicom the last time our paths crossed.


First off, yes, "minicom" is a POS, and I cry every time I hear it recommended to a newbie.
Or see a newbie using it (because he read somewhere that it was a good choice for a serial comm program in Linux). I feel it is my duty to point out that a) minicom is a POS and b) an easy, cheap alternative exists (screen).

Second, I don't "back up my arguments" because I am not here to argue with you. You obviously believe what you believe and I doubt I can change that, nor do I have any desire to do so. I am here for the newbies.

Finally, I can think of 3 obvious reasons why using cron as you describe is a bad idea. They are so obvious that I won't bother writing them out now. Maybe at some point, when I'm really bored, I may type them out (and paste them in here).

And that's that...
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2589
Joined: Sun Jan 15, 2012 1:11 pm
by pluggy » Sun Aug 18, 2013 6:49 pm
I won't start on what I think of your 'baby' putty......

Why is screen cheaper than Minicom ?. They are both built into the standard repositories. I find screen confusing because its a lot more than comms software - fantastically flexible. As has been mentioned by others minicom is pretty much a clone of Telix, which is pure old school comms software. Takes me right back to playing on dial up bulletin boards in the 80's :)

If you want really bare bones comms you use stty and cat/head/tail/echo
Don't judge Linux by the Pi.......
User avatar
Posts: 2402
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by Joe Schmoe » Sun Aug 18, 2013 7:39 pm
baby putty???
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2589
Joined: Sun Jan 15, 2012 1:11 pm