There are loads of ways of running a command at start-up in Linux but my favoured approach is to create an initialisation script in /etc/init.d and register it using update-rc.d. This way the application is started and stopped automatically when the system boots / shutdowns.
Create script in /etc/init.d
sudo nano /etc/init.d/NameOfYourScript
The following is an example based on starting up the no-ip service [/usr/local/bin/noip], but change the name of the script and the command to start and stop it and it would work for any command.
#! /bin/sh
# /etc/init.d/noip
### BEGIN INIT INFO
# Provides: noip
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting noip"
# run application you want to start
/usr/local/bin/noip2
;;
stop)
echo "Stopping noip"
# kill application you want to stop
killall noip2
;;
*)
echo "Usage: /etc/init.d/noip {start|stop}"
exit 1
;;
esac
exit 0
# /etc/init.d/noip
### BEGIN INIT INFO
# Provides: noip
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting noip"
# run application you want to start
/usr/local/bin/noip2
;;
stop)
echo "Stopping noip"
# kill application you want to stop
killall noip2
;;
*)
echo "Usage: /etc/init.d/noip {start|stop}"
exit 1
;;
esac
exit 0
Warning - its important you test your script first and make sure it doesn't need a user to provide a response, press "y" or similar, because you may find it hangs the raspberry pi on boot waiting for a user (who's not there) to do something!
Make script executable
sudo chmod 755 /etc/init.d/NameOfYourScript
Test starting the program
sudo /etc/init.d/NameOfYourScript start
Test stopping the program
sudo /etc/init.d/NameOfYourScript stop
Register script to be run at start-up
To register your script to be run at start-up and shutdown, run the following command:
sudo update-rc.d NameOfYourScript defaults
Note - The header at the start is to make the script LSB compliant and provides details about the start up script and you should only need to change the name. If you want to know more about creating LSB scripts for managing services, see http://wiki.debian.org/LSBInitScripts
If you ever want to remove the script from start-up, run the following command:
sudo update-rc.d -f NameOfYourScript remove
Hi Martin,
ReplyDeleteThanks for the Blog! I've tried using your code above to run no-ip on start-up (I have the Raspberry Pi B Model running Debian Squeeze) but keep getting the error below when trying to run it:
"11: Syntax error: word unexpected (expecting ")")"
I've installed the same version of no-ip as you have above, and have installed it in the same location. Any ideas on what is causing this error?
I've done a bit of reading and think i wonder what O/S you are using as it may be that you have writted your code for something other than Debian.
Cheers,
James
Hi James,
DeleteI don't know what to suggest, I am using the Debian Squeeze distro (19-04-2012). To make sure I have just recreated this script on my pi copying and pasting the script from above and I didn't experience any errors.
Have you made any changes to the distro, installing dev libraries anything like that which might be causing an issue?
Sorry I cant be of more help.
Mart
James,
DeleteSomething came to mind, have you included the #! /bin/sh directive at the top of your script? This tells the linux program loader to use the /bin/sh interpreter, although its ignored by the script itself. If you have, check that /bin/sh exists and its not in a different directory.
Mart
I had same problem. I found that when you copy and paste from the web site you get some hidden characters in the white spaces.
DeleteThis can be fixed by deleting all if the white space indenting on each line... Even the spaces before any # must be deleted. You can put back the spacing using space bar if you wish.
Also found that if I use:
sudo /etc/init.d/xxx start
The script works but the imbedded program never starts without any indication. Starting without using sudo works fine. Must use sudo to stop the program.
Kevin
Its because line 10 of what you copied wrapped to line 11. Joing the two lines or start line 11 with the octothorpe ('#').
DeleteHi Martin,
ReplyDeleteThanks for the help, i did have the #!/bin/sh and it was in the right directory. I found another script similar to yours above, except with the addition of
"User:root
UserHome:/root/home/
$Export USERINFO"
(or something along those lines)
Which allows me to run vnc and noip on startup!
However, with the downside of having to startup my vnc server as root, which isnt ideal.
Im thinking maybe i have some permission problems, but can't seem to figure out what is wrong.
Thanks for the help.
James
Thanks! works fine! :)
ReplyDeleteHi Martin,
ReplyDeleteI might have an issue here. I used this script to loop a video on startup (works great!) with OMXPlayer using the same code as they used here:
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=10218&p=115800
**************
#!/bin/sh -x
SERVICE='omxplayer'
while true; do
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "runing" # sleep 1
else
omxplayer /usr/local/bin/LTC.mp4
fi
done
**************
When I stop the video (Q) the shell code starts the video again.
It works perfect for the initial idea (fool-proof plug and play video loop for exhibition, except for the small gap between end&start), but I wonder if there are options to get out without needing to format the card :)
If any of you have some suggestions, except for typing
"sudo update-rc.d -f NameOfYourScript remove" in a second..
Thanks!
Paul
Martin, great post! This worked right out of the box for me. Two questions for you as I'm a bit of a UNIX noob:
ReplyDelete1) If I should have a typo in my script, will that cause the entire boot process to stop or will it just ignore the bad script? IOW, will I be able to easily fix the bug?
2) I'm running my NodeJS server at start up and I see the following processes when I check:
root 1919 1 4 10:56 ? 00:00:03 node /var/www/vevo/app.js
root 1921 1 0 10:56 ? 00:00:00 startpar -f -- nodejs
root 2286 2275 0 10:57 pts/0 00:00:00 grep node
Now, the first one and the last one I'm expecting but what is the second one?
Thanks,
- MT
BTW, my script is named "nodejs" (all lowercase).
Delete1) No, if you have an error in your script it wont stop the boot process, you will probably see the error on screen when your Pi boots up. I would recommend you test your script before commiting it to run at boot by using /etc/init.d/nodejs start - this will show you whether it works or not
Delete2) Not a clue, I know nothing about nodejs or how it runs, startpar is a linux utility used to run scripts in parallel, but thats where my insight ends!
Thanks Martin! Script was tested as per your instructions above and it works great!
DeleteHello Martin, thanks for your blog!
ReplyDeleteI followed your steps to make my python script auto run after raspberry pi power on but got some error message when run command "sudo update-rc.d lcd12864 defaults":
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'Fonts.pyc' missing LSB tags and overrides
insserv: warning: script 'Fonts.py' missing LSB tags and overrides
insserv: warning: script 'ST7565.pyc' missing LSB tags and overrides
insserv: warning: script 'main.py' missing LSB tags and overrides
insserv: warning: script 'ST7565.py' missing LSB tags and overrides
I googled the error message and got this link http://wiki.debian.org/LSBInitScripts, but after i did what it said i still got the same error message after run that command.
Do you have some ideas of this? thanks in advance!
They are warnings letting you know you have scripts in init.d which aren't LSB compliant. They look a bit scary but they wont affect the script and it will still work. Creating LSB compliant scripts for a simple startup program is overkill and quite frankly a pain in the backside. So, in summary, my advice would be "dont worry".
DeleteYes, as you said, I just test that my python script can auto run after power on. Even if i got warnings message when add it to the init.d. Thanks very very much! :)
Delete@Martin - thank you very much. This is precisely what I was looking for! Works great.
ReplyDeleteNo worries
DeleteMartin, Thanks for this explanation.
ReplyDeleteYet: Now I have put it in, it works only sometimes (Random if it works or not).
The script I am using is also using an SQL database (it's a thermostat that start/stop the heating, and it is controlled remotely by writing values in a MySQL. The script then looks at the database to see if it should enable/disable the heating in the house).
As far as I can see it seems that the SQL server sometimes comes up later then the script, causing the python-script to stop because it cannot connect.
My question therefore: Can you actually control which service should start first or last? Would it make sense to have a pause at the start of the script?
You have some control over when your script runs by modifying the Required-Start tag in the LSB header, you should be able to add $all to the tag (after $syslog) which tells the script to run after all other scripts.
DeleteSee http://wiki.debian.org/LSBInitScripts for more info.
Hey Martin,
DeleteThanks for the reply; I now now where to look!
I have implemented it over SSH remotely, so I will be able to see if it works only once I am at the Raspberry, to see if the GPIO is really functioning. Yet, I am confident it will!
Thanks again!
Hi Martin I tried the above for iPLayer and it doesn't seem to start it automatically.
ReplyDeletewhen running (I called the file iPlayer)
sudo /etc/init.d/iPlayer start
sudo update-rc.d NameOfYourScript defaults
both run without errors and iPlayer starts, but after reboot this has to be run manually, I copied your script about and replaced
/usr/local/bin/noip2
with
perl ./get_iplayer-2.82/get_iplayer.cgi --port=1935 --getiplayer=./get_iplayer-2.82/get_iplayer 2>> /logfile/path/logfile.log &
does it not like perl on bootup?
Regards
John
Hi John,
ReplyDeleteI think you problem is due to 'relative' paths, when you use ./ it effectively says use "my current directory". dot [.] is your current directory dot dot [..] is the parent directory.
When your command ./get_iplayer-2.82/get_iplayer.cgi runs at start-up the current directory will be different and your program wont be found.
So I think you will be able to solve your problem by using full paths (which is always a good idea when creating scripts), if get_iplayer is installed in your home directory (/home/pi), referred to as ~, the following should work
perl /home/pi/get_iplayer-2.82/get_iplayer.cgi --port=1935 --getiplayer=/home/pi/get_iplayer-2.82/get_iplayer 2>> /logfile/path/logfile.log &
Let me know how you get on.
Mart
Martin - thanks very much for your help.
ReplyDeleteI can confirm this is now loading on boot perfectly
killall -> pkill
ReplyDeleteHello,
ReplyDeleteI'm having a little problem with my script. I wanted to test if a simple script that I already had written starts on boot. It does, however I didn't think about including a way to stop the script and now I can't log in to my Raspberry. CTRL+C doesn't work as well as any other commands I tried. I tried searching for a way to stop the script manually but I didn't find anything usefull. Any suggestions?
Im surprised you cant login - What does the script do? Is it waiting for a user input? Has it hung while starting up?
DeleteCan you login to the pi via ssh from another machine? If you can I would use sudo update-rc.d -f NameOfYourScript remove, to remove the script from the startup process and reboot. If you cant login at all, Im stumped, you might be able to modify the contents of the SD card if you have a linux pc and delete your script from /etc/init.d.
Let me know how you get on.
I tried everything I could think of, everything I found with google, I even asked a friend of mine who is a linux expert, but nothing worked. The script was waiting for user input so basically none of the linux commands worked.
DeleteI didn't set up SSH either so that wasn't an option.
So the only option left was to back up all the important data from my SD card and reinstall Rasbian.
Leasons learned: always include a failsafe in your code.
Oh yeah, almost forgot. I reinstalled Rasbian because I didn't have a linux pc at my disposal and for Windows I could only find a ext reader application, no write permission.
DeleteIll update the post to put a warning about making sure your script doesn't hang waiting for a user input which wont ever happen.
DeleteLike Marko I ended up with a non interrupable script running preventing me doing anything and only option I could do was to rebuild SD card. Therefore I would appreciate your post update you mention above and any tips on how to run a prpgram at startup in the background. Or what not to do!
ReplyDeleteMany thanks!
Thank you very much!
ReplyDeleteI tried this , but when the ip address changes it does not work . I can't access it online (it's not recognizing the change in ip)
I thought it might be a problem with the startup so I went to run it it said it already is running ..
Do you have any idea what might be the problem?
Thanks for the post, but I just can't get mine to work on boot!
ReplyDeleteI want to open iceweasel on startup and have written the following script called startweasel:
-------------------------------------
#! /bin/sh
# /etc/init.d/startweasel
### BEGIN INIT INFO
# Provides: iceweasel
# Required-Start: $remote_fs $syslog $all
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
echo "Starting Iceweasel"
iceweasel
# Carry out specific functions when asked to by the system
exit 0
-------------------------------------
The script runs fine but doesn't run at startup. I have run "sudo update-rc.d startweasel defaults"
Do you have any suggestions?
I think the problem is that it S04 on the start whereas x11 is S16 so it's trying to start the browser before the GUI. I've tried running "sudo update-rc.d startweasel defaults 17 17" but it still comes up as S04
DeleteHi Miles, have you had any luck? I've never tried to start up X programs using a init.d script, so sorry Im not a lot of help.
DeleteHello Martin,
ReplyDeleteHow can you build in dependencies, i have to wait till apache is started before my specific program may start.
regards,W
You have some control over when your script runs by modifying the Required-Start tag in the LSB header, you should be able to specify the apache service here, although it might be easier to add $all to the tag (after $syslog) which tells the script to run after all other scripts.
DeleteSee http://wiki.debian.org/LSBInitScripts for more info.
hello Martin
ReplyDeleteI start noip at boot (works) but when I shutdown my rpi, it halts. I guess that noip doesn't properly be killed.
My script is the same as yours.
I tried killall en pkill but nothing.
Regards DM
Sounds odd are you sure its noip which is halting the shutdown?
DeleteIf you use killall noip from the command prompt does that work?
This comment has been removed by the author.
DeleteOk problem is fixed but I don't know how.
DeleteI didn't change a thing on the script and now everything works.
Thanks for helping
what do you change??
DeleteHi Martin,
ReplyDeleteIm trying to start a bluetooth dongle which auto starts on boot and scans for other bluetooth devices. I used your code to start it on boot up. The code works perfect but it shows an error during boot
"Error Initializing the bluetooth device"
But once i login into the pi and run the program, it works!
I guess the bluetooth is only started after the init script is run. I tried adding $all but still it doesnt work. Pls help me!!
@Jijo Mathew: I have the same problems with hictool (Bluez). Did you find the solution?
DeleteHey Martin!
ReplyDeleteI am trying to make my passcode program run on startup, and I tried this. The program runs correctly, but the RPi still starts up as normal and does not wait for the program to finish. Is there a way for it to not boot into the GUI until the program has finished running (the passcode is correct)?
I dont know! Ive had a quick read of the debian lsb init scripts documentation and it suggests that you can put the tag X-Interactive: true into the header to stop the program running in parallel and give the user acces to the console.
DeleteI havent tried it so have a go add # X-Interactive: true just before the ### END INIT INFO line. One word of catuion though... Make sure you have got stuff backed up, a few guys have said that they have been locked out f their pi when experimenting with the boot sequence. But if we dont try we dont learn!
i have found this post useful in finding out a way to create a automatic method to connect the pi to a 3g internet via sakis3g script. the details are posted in
ReplyDeletehttp://goo.gl/w01Bdz.
thanks for the info
Hi Martin,
ReplyDeleteThank you for being so active in responding to these questions. I have an issue I wasn't able to find answered above (though I may not have recognized the answer - I'm new to linux). I'm running Debian on a Raspberry Pi as a server in my home. (lightweight, but fun to experiment with). I've installed Google Cloud Print and Chromium, and am able to print from my Android devices through the RPi IF I manually start Chromium after boot in an ssh window. (chriomium -- type=service) This actually works without the X environment started.
The problem (at least in my mind) is that this does not 'complete' and return to a command line prompt. It will just monopolize the session and never return a command prompt.
Since this command never actually completes, can I use an init.d script?
Thanks
Hi,
DeleteYou can run any command in linux in the background by adding a & to the end of the command e.g chromium --type=service & .
This is known as a daemon. Init.d scripts are designed to run these types of commands. S put an ampersand, &, at the end and all should be good.
Mart
Maby this might seem as a rather strange question, but what programming language is the script written in? It looks like the one you can use in windows to.
ReplyDelete(compeletely new to the Linux and the Raspberry universe.)
Er, I would just call it a bash script. If you're used to windows its the equivalent of a command (aka batch) file.
DeleteThank you for your nice explanation.
ReplyDeleteI just wrote a small tutorial about starting applications at boot time with root privileges in Debian.
You can find it in here:
http://cosmolinux.no-ip.org/raconetlinux2/boot.html
I wish it is useful to someone.
I tried your tutorial but when I try to start the program it says "permission denied". Any ideas how to fix that?
ReplyDeleteAre you using sudo to run your program?
DeleteThanks!!
ReplyDeleteRight, massive problem here, I run this to operate a looping python script, which works, however, now I cannot connect via ssh or anything (Connection refused), because whilst this is looping, it seems to stop it from continuing on with the boot phase, is this right? What can I do?!?!?!?!?!?!
ReplyDeleteYou need to put an ampersand & on the end of your command e.g. python myprogram.py & . To make it run in the background. Otherwise the boot sequence will wait for the command to finish.
ReplyDeleteGreat tutorial. I am trying to implement this for a node.js app. If I manually run it it will run just fine, but I can't get it start on boot. Any suggestions?
ReplyDeleteAre you getting an errors? It might be worth writing the output to a file. E.g. node.js ###### >> /home/pi/logfile.txt.
DeleteOne thing, make sure you are using full paths in your commands. e.g. /home/pi/file not ~/file or file.
Thanks a lot! Did work for me.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, thanks for the post!
ReplyDeleteI can get messages that my startup script is running, but I need to run the command as the "pi" user. I have tried runuser, but that does not work. Help would be greatly appreciated
Have you tried executing your command with:
Deletesu -c /path/to/command pi
Hi Martin,
ReplyDeleteIs it possible to run a pi cam to make a time lapse video when the pi boots up, and then shut down when it has finished? What if you want to remove it when you have finished the project? Thanks
Sorry I just found your blog about time lapse video. Thanks. Great stuff
DeleteAlternatively you can just:
ReplyDeletesudo crontab -e
.. and add the following line:
@reboot sudo /usr/local/bin/noip2
Hi great post but it's not quite working for me. When I run "sudo /etc/init.d/NameOfYourScript start" this will not work unless I then quit the terminal. Any thoughts? Thanks
ReplyDeleteAhhhh, answer to my own question! I missed the # from the first line #! /bin/sh.
ReplyDeleteI want to run Python program which uses communication
ReplyDeletei.e. Serial comm and ports
Please give me steps
To make this happen
1) open google
Delete2) find resources for python serial programming
3) learn
4) code
Martin,
ReplyDeleteI've attempted, and so far failed to pinch your script after getting it to work perfectly with noip, with cuberite. I have changed the various bits as follows:
#! /bin/sh
# /etc/init.d/cuberite
### BEGIN INIT INFO
# Provides: cuberite
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting cuberite"
# run application you want to start
cd Server
./Cuberite
;;
stop)
echo "Stopping cuberite"
# kill application you want to stop
killall Cuberite
;;
*)
echo "Usage: /etc/init.d/cuberite {start|stop}"
exit 1
;;
esac
exit 0
A shameless copy I know but without it I'd be stumped...So, the script starts and stops, or rather does give any errors but the server doesn't start.
I've spent a good few hours googling but am lost. I think it may be down to the ./Cuberite bit as I've created a simple test script to try and start it as a start.sh but this won't start without being run as ./start.sh.
Any pointers would be ace.
ps I bought your book so I'm attempting to appeal to your better nature ;-)
Lol...
DeleteI suspect your program is that you need to specify a full path when calling cuberite.
i.e. rather than:
cd Server
./Cuberite
do:
/home/pi/Server/Cuberite
(if the Server directory is in /home/pi)
This is because when the Pi boots, its being booted by the admin user, not the pi user and the directory context isnt ~ (like when you launch a terminal)
Thanks Martin. I'll keep exploring. Old dogs can learn new tricks
ReplyDeleteHi Can you help.
ReplyDeleteIm getting the following error when I run my script.
pi@raspberrypi ~ $ sudo update-rc.d hivekit defaults
update-rc.d: using dependency based boot sequencing
insserv: Script HiveKit is broken: incomplete LSB comment.
insserv: missing valid name for `Provides:' please add.
The header to my code looks like this.
#! /bin/sh
# /etc/init.d/hivekit
### BEGIN INIT INFO
# Provides: hivekit
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot /
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting hivekit"
# run application you want to start
/home/osmc/hivekit/bin/hivekit -username gwhite1@me.com -password K8white1988 -pin 80808080 &
;;
stop)
Thanks for any help.
Cheers
Gary
If that is your complete script, it seems to be missing half of it, it should have entries under stop.
DeleteI cant see anything particularly wrong with the header. the only slightly odd bit was the use of mixed case for HiveKit and hivekit - i dont know if that would make a difference tho.
Hi Martin,
ReplyDeleteI'm using the steps you describe here to start my timelapse script on a Raspberry Pi, but when I reboot the pi, raspistill doesn't start. If I start it manually via "sudo service timelapse start" everything runs as expected. Do you have any suggestions about debugging this situation? I don't see anything in /var/log/syslog, and am not sure where else to look.
Thanks for any info,
Phil
I found my problem. In the script that actually starts raspistill, I was testing for the existence of a directory (on a usb stick, where I want to store the images), and the test was always failing, so raspistill was never starting. Not sure why that particular test is failing; that's the next thing to look at.
ReplyDeleteThanks for your excellent instructions here--they are just what I wanted.
Maybe your script is running before the USB stick gets mounted?
DeleteFantastic post- worked for me and DynamicDNS right off the bat!
ReplyDeleteThanks for this Martin,
ReplyDeleteYou were an absolute inspiration at Picademy earlier this year and now your fabulous blog has helped me to write my first 'run at startup' script. I was slightly lost and it didn't actually work... but it was a triumph nonetheless!
Thank you,
Pablo
So this may sounds stupid but how would you go about testing before you make the script executable. It's my first time doing any of this and I don't want to like break the computer or something. Thank you
ReplyDeleteYou will need to make your script executable before you can run and test it. In my experience its pretty difficult to break a computer. If you are worried, take your SD card out of the Pi, stick it in a computer, create an image of it as a back up, and if the worst comes to the worst you can just re-image your SD from your backup.
DeleteHi firts thanks you, this help me a lot but
ReplyDeleteMy problem: when i follow the steps and i reboot the raspPI it works find but when i shutdown the raspPI doesnt work any more
What i try: Google the problem, find syntaxis errors, change the permisions, corret the python scripts
Doudts: i try to star de script but doesnt do anything
Thanks for the good explanations!
ReplyDeleteI'm trying to get usb_modeswitch to run on reboot. I have made a script with the help from this article. It seems to work, but to early to be able to do anything. Actually it's the second first output to /var/log/messages after reboot:
Mar 15 13:37:27 navio rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="515" x-info="http://www.rsyslog.com"] exiting on signal 15.
Wed 15 Mar 13:37:30 UTC 2017
ff Script without spec
ff Starting usb_modeswitch ff
Mar 15 13:37:30 navio rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="522" x-info="http://www.rsyslog.com"] start
Mar 15 13:37:30 navio kernel: [ 0.000000] Booting Linux on physical CPU 0x0
Mar 15 13:37:30 navio kernel: [ 0.000000] Initializing cgroup subsys cpuset
Mar 15 13:37:30 navio kernel: [ 0.000000] Initializing cgroup subsys cpu
Mar 15 13:37:30 navio kernel: [ 0.000000] Initializing cgroup subsys cpuacct
Mar 15 13:37:30 navio kernel: [ 0.000000] Linux version 4.4.6-4d0ae01-emlid-v7+ (root@kernelcruncher) (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu1) ) #3 SMP PR$
This is what the script looks like:
#! /bin/sh
### BEGIN INIT INFO
# Provides: usb_modeswitch
# Required-Start: $remote_fs $syslog $all
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to force usb mode swich
# Description:
### END INIT INFO
(date && echo 'ff Script without spec') >> /var/log/messages
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "ff Starting usb_modeswitch ff" >> /var/log/messages
# run application you want to start
/usr/sbin/usb_modeswitch -c /etc/usb_modeswitch.conf
;;
stop)
echo "ff Stopping nothing ff" >> /var/log/messages
# kill application you want to stop
;;
*)
echo "ff Other ff" >> /var/log/messages
exit 1
;;
esac
exit 0
If I put the action in /home/pi/.bashrc it works but I would like it to work before login.
Grateful for any tipps! /Fredrik
I've been following this tutorial which references your script:
ReplyDeleteThe script works great but when I do the [sudo update-rc.d autoconnectnet defaults] command it doesn't work... can you give any advice please? I assume I've done something wrong but i dont get an error so can't guess what
https://lawrencematthew.wordpress.com/2013/08/07/connect-raspberry-pi-to-a-3g-network-automatically-during-its-boot/
Hi,
ReplyDeleteit works fine and instruction are very clear !
One question : how would you make the script running as pi user ? mine is running as root. I'm quite new in linux so i can't get it ...
thanks,
Guillaume
Im not sure. All init.d scripts are run as root as that probably cant be changed.
DeleteIn the init.d script could you run your script using runuser (https://www.cyberciti.biz/open-source/command-line-hacks/linux-run-command-as-different-user/)?
thanks so much!!
ReplyDeleteHi Martin, I used the version with init.d and it almost works. If I start the script with command "sudo /etc/init.d/NameOfYourScript start", I can see the output of my script on tty (which is what I want). If I enter the command "sudo update-rc.d NameOfYourScript defaults", I know the script started, because it beeps at startup, but I can't see the text messages. How can I direct the output to tty?
ReplyDeleteHello Martin, I am writing a python script to measure temperatures and would like my pi zero to start the script when it boots up. After looking at your code to start the no-ip program, I am wondering how to start the python script that usually requires "sudo python Temperature.py" in bash. Should the I amend youur "sudo chmod 755 /etc/init.d/NameOfYourScript" to include the python Temperature.py?
ReplyDeletechmod 755 makes a script executable. In your example 'python' is the program and it is already executable so you dont need to.
DeleteMatin, I have tested my script and it works from bash but when I try to run sudo /etc/init.d/Thermometer.py I get the message below.
ReplyDeleteStarting Thermometer.py
/etc/init.d/Thermometer.py: 24: /etc/init.d/Thermometer.py: /home/pi/Thermometer.py: Permission denied
Did you make the script executable?
DeleteYes I did. I used sudo chmod 755 /etc/init.d/Thermometer.py
ReplyDeleteWell, did some more reading of your script. I left out the "# If you want a command to always run, put it here", I put sudo python Thermometer.py and now the script runs sort of. My py program reads an attached temperature sensor every two minutes and sends it to an IoT site for display. When I put in sudo /etc/init.d/Thermometer.py start, my program will go thru one cycle and then stop. I also do not get the echo statement.
DeleteAdded an & at the end of sudo python Thermometer.py and now it runs. I am getting the echo statement along with an error message, but the script runs anyway. Go figure. Thanks
ReplyDeleteHi,
ReplyDeleteI'm having a little issue with my content. I needed to test if a straightforward content that I as of now had composed begins on boot. It does, be that as it may I didn't consider including an approach to stop the content and now I can't sign in to my Raspberry. CTRL+C doesn't fill in and additionally some other summons I attempted. I had a go at hunting down an approach to stop the content physically yet I didn't discover anything valuable. Any proposals?
Regards,
Trep Helix
Thx a lot man, your snippet very helpful and easy to use!
ReplyDeleteHello Laurent:
ReplyDeleteI have a problem that can not be solved, I'll explain.
In Init.d I have the script set and it works when booting, but the strange thing is that everything works, except for a UDP sending parameter.
The command that I tell you to execute is this .....
./acarsdeco2 --gain 49 --freq-correction 15 --freq 131511000 --freq 131711000 --freq 131811000 --outConnectUdp pp: 192.168.1.128: 9742 --http-port 8998
Everything works except --outConnectUdp pp: 192.168.1.128: 9742
Well the funny thing is the following, if I run it through the console, it works perfect, if I execute it via init, d, that parameter fails.
What could happen?
Thank you very much.
I'm sorry I'm wrong in the name, the right thing is ...
DeleteHello Martin..
thanks man, really helpful post, thumbs up
ReplyDelete