Page 1 of 1
How to run a script on Raspbian boot?
Posted: Thu Jan 09, 2014 1:16 pm
by vijamaica
Sorry folks if this is a stupid question, but how can I run a script right after system boots?
I have a script for dropbox syncronization with my local folder and I'd like to have it running as a service since the system is up.
I've already tried many different ways as described below but all them failed.
First I tried to add the script with a & at the end of the command line into rc.local file. No luck, for some reason it stuck and it does nothing.
Second I tried to create another start/stop script and add it into init boot sequence as suggested at
http://www.debian-administration.org/articles/28, but no luck again, it stucks again and nothing works.
Now I removed the script from init sequence and after every boot I have to manually log in and run sudo service dropbox start command to have it working, and now it does work.
Any idea how to fix this is welcome.
In case you need further details just let me know, tks.
Re: How to run a script on Raspbian boot?
Posted: Thu Jan 09, 2014 10:48 pm
by AndyD
Try looking in /var/log/messages and using dmesg to see what is going on. If you say it gets stuck try and work out were. If you run the script manually does it still get stuck?
If you are looking for another method to run scripts at startup. Look at
cron/crontab and @reboot.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 10:56 am
by vijamaica
Thanks for the tip AndyD
I'll check the messages and the cron option and will let you know if it works.
Replying your question, when I manually run the script it works fine, even if after the boot I manually start the service (sudo service dropbox start) it runs fine. It stucks just if the script run on the boot process.
Thanks again!!
AndyD wrote:Try looking in /var/log/messages and using dmesg to see what is going on. If you say it gets stuck try and work out were. If you run the script manually does it still get stuck?
If you are looking for another method to run scripts at startup. Look at
cron/crontab and @reboot.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 1:38 pm
by atao3
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 1:39 pm
by Joe Schmoe
Note that this (the site you reference) is written in some other language.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 2:19 pm
by DougieLawson
Joe Schmoe wrote:
Note that this (the site you reference) is written in some other language.
Makes perfect sense to me, despite being in technical French.
http://translate.google.com is your friend if you can't read French unaided.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 3:22 pm
by vijamaica
Thanks folks for all your comments.
I already tried the suggestion from the French site, but it stucks on the same way.
I used now the crontab option calling the service I created into init.d and now it's running fine.
I'll try check the messages as sugested to understand the real issue, but it's not urgent as it's running ok after every boot.

Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 3:35 pm
by Richard-TX
/etc/rc.local is the last script that runs so adding your script there makes sense.
if you need more time, add a sleep to your script and then run your script in the background from rc.local. (/path/to/yourscript &)
If you do that you might need to add nohup to the beginning. (nohup /path/to/yourscript &)
Do not include parens.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 4:01 pm
by Joe Schmoe
What's wrong with parens? What do you have against them?
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 6:33 pm
by jojopi
Richard-TX wrote:If you do that you might need to add nohup to the beginning. (nohup /path/to/yourscript &)
nohup is for starting long-running jobs in an interactive shell so that they are disconnected from the terminal and immune to future communications failures. screen or tmux are more elegant solutions to this problem.
You do not need to nohup background tasks in a script, because they are not killed when the script ends. You should never use nohup in an init script, because it will litter the root directory with "nohup.out" files.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 7:12 pm
by Richard-TX
I have had to use nohup depending on certain conditions. That is why I said he might have to use it.
Responsibility for the use of nohup is firmly placed in the hands of the person using the command. Having said that "nohup cmd 2>&1 > /dev/null & " resolves the problem of nohup.out files as they will not get created.
screen and tmux don't exist by default and is totally unavailable on some systems so I did not mention them.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 8:59 pm
by DougieLawson
Richard-TX wrote:/etc/rc.local is the last script that runs so adding your script there makes sense.
Doing it with a /etc/init.d script and update-rc.d give you more control and isolates each long running task with it's own controls and, if needed, different runlevels where it's active (yes I know Raspbian only uses runlevels S, 0, 2 & 6), I still wonder why they didn't add runlevel 3 with the X-windows active with a login manager (rather than getting the novice users to type startx).
I guess I'm biased because of my experiences building LinuxfromScratch on an old SuSE box and having to work with all the /etc/init.d scripts.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 9:12 pm
by Richard-TX
DougieLawson wrote:Richard-TX wrote:/etc/rc.local is the last script that runs so adding your script there makes sense./quote]
Doing it with a /etc/init.d script and update-rc.d give you more control and isolates each long running task with it's own controls and, if needed, different runlevels where it's active (yes I know Raspbian only uses runlevels S, 0, 2 & 6), I still wonder why they didn't add runlevel 3 with the X-windows active with a login manager (rather than getting the novice users to type startx).
I agree but given the nature of the question and apparent level of expertise of the OP, I decided to give him a solution that is as simple as possible yet works. Put another way, if he had googled "raspberrt start scripts" he would have found the same info I gave.
I just did the google search I mentioned and the third result returned the following:
http://raspberrywebserver.com/serveradm ... rt-up.html
Guess what is at the top of the list?
I have no idea why Raspian decided to offer so few run levels. I still prefer the System V way of starting a system. Simple, robust, easy to admin, and reliable. I guess I am a little prejudiced having spent so time working at AT&T USL.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 9:44 pm
by rpdom
Richard-TX wrote:I have no idea why Raspian decided to offer so few run levels. I still prefer the System V way of starting a system. Simple, robust, easy to admin, and reliable.
It is not because of any Raspbian policy to offer a sane number of run levels. It is the Debian standard. Raspbian is Debian rebuilt and optimised for the Pi.
I'm not going to comment on the old sysv method of startup.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 9:50 pm
by Richard-TX
rpdom wrote:Richard-TX wrote:I have no idea why Raspian decided to offer so few run levels. I still prefer the System V way of starting a system. Simple, robust, easy to admin, and reliable.
It is not because of any Raspbian policy to offer a sane number of run levels. It is the Debian standard. Raspbian is Debian rebuilt and optimised for the Pi.
I'm not going to comment on the old sysv method of startup.
I do so love standards. There are so many to choose from.
SysV was a little archaic, but it was still better than SunOS.
Raspbian seemingly uses the SYSV model (/etc/rc[0-6].d/... It is a mess but it is there.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 10:01 pm
by Richard-TX
Debian says that it supports 7 run levels. By default it makes no distinction between 2-5
https://wiki.debian.org/RunLevel
Dougie,
Do you want to write a patch so that run level 3 (X Win) is enabled? The command patch makes sense to me.
Richard
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 10:09 pm
by DougieLawson
Richard-TX wrote:Do you want to write a patch so that run level 3 (X Win) is enabled? The command patch makes sense to me.
I'll leave it for you. The number of times my RPi has ever run a graphical interface can be counted (in binary) on one hand. I'll normally use VNC if I have something that uses a GUI.
We don't need no stinking GUIs/Xwindows/desktop cr*p.
Re: How to run a script on Raspbian boot?
Posted: Fri Jan 10, 2014 10:20 pm
by Richard-TX
I run xwin on one of my Rpis but only as a remote monitor for my XP machine (VNC).
I don't need no stinkin' gui either.

Re: How to run a script on Raspbian boot?
Posted: Sat Jan 11, 2014 7:45 am
by rpdom
The only system I run a GUI on (out of the 15+... actually I'm not even sure how many I've got including may little laptops) is the Debian laptop I'm using right now. Everything else is command line and mostly headless.
Re: How to run a script on Raspbian boot?
Posted: Sat Jan 11, 2014 8:03 am
by Richard-TX
rpdom wrote:The only system I run a GUI on (out of the 15+... actually I'm not even sure how many I've got including may little laptops) is the Debian laptop I'm using right now. Everything else is command line and mostly headless.
That pretty much defines my home network as well.
Who could have predicted 10 years ago that server farms would be so cheap and so common that many people would have one?
Distributed Computing (aka Cloud) has finally made it to the average Joe. If this keeps up, I might have to start routing networks internally within the next 2 years.
Re: How to run a script on Raspbian boot?
Posted: Sat Jan 11, 2014 9:19 am
by jojopi
DougieLawson wrote:I still wonder why they didn't add runlevel 3 with the X-windows active with a login manager (rather than getting the novice users to type startx).
Richard-TX wrote:Do you want to write a patch so that run level 3 (X Win) is enabled?
What change are you actually proposing here? To have the display manager start in runlevel 3 is just:
But having novices log in and then change run level, then possibly log in again graphically, does not seem better than startx. And running the display manager in runlevel 3
and making that the default runlevel, is effectively equivalent to running it in runlevel 2, which is what raspi-config already does for boot to desktop mode.
Re: How to run a script on Raspbian boot?
Posted: Sat Jan 11, 2014 8:19 pm
by DougieLawson
I think they should be doing that in the shipped versions of raspbian and NOOBS. (They being the Foundation.)
They can use raspi-config to update /etc/inittab with a new default runlevel and have runlevel 3 do the graphics stuff with runlevel 2 sticking as it is today.