vijamaica
Posts: 3
Joined: Thu Jan 09, 2014 1:04 pm

How to run a script on Raspbian boot?

Thu Jan 09, 2014 1:16 pm

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.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: How to run a script on Raspbian boot?

Thu Jan 09, 2014 10:48 pm

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.

vijamaica
Posts: 3
Joined: Thu Jan 09, 2014 1:04 pm

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 10:56 am

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.


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

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 1:39 pm

atao3 wrote:Look this how run script on startup
Note that this (the site you reference) is written in some other language.
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
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 2:19 pm

Joe Schmoe wrote:
atao3 wrote:Look this how run script on startup
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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

vijamaica
Posts: 3
Joined: Thu Jan 09, 2014 1:04 pm

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 3:22 pm

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.

:D

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 3:35 pm

/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.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

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

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 4:01 pm

What's wrong with parens? What do you have against them?
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
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 6:33 pm

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.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 7:12 pm

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.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 8:59 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 9:12 pm

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.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 9:44 pm

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.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 9:50 pm

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. :D

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.
Last edited by Richard-TX on Fri Jan 10, 2014 10:07 pm, edited 1 time in total.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 10:01 pm

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
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 10:09 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Fri Jan 10, 2014 10:20 pm

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. :D
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to run a script on Raspbian boot?

Sat Jan 11, 2014 7:45 am

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.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to run a script on Raspbian boot?

Sat Jan 11, 2014 8:03 am

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. :D

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.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: How to run a script on Raspbian boot?

Sat Jan 11, 2014 9:19 am

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:

Code: Select all

sudo update-rc.d lightdm enable 3
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.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to run a script on Raspbian boot?

Sat Jan 11, 2014 8:19 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Raspberry Pi OS”