Auto start a script


20 posts
by RaTTuS » Fri Jun 01, 2012 11:34 am
if you want to start a script off when your RPi boots up there are various ways but here is one that is very useful.
1) make sure your script runs [this is always useful ;-p]
2)edit /etc/inittab with your favourite root mode editor [I use sudo vi /etc/inittab YMMV]
2a) find the line
1:2345:respawn:/sbin/getty 115200 tty1
and replace it with
1:2345:respawn:/bin/login -f USERNAME tty1 </dev/tty1 >/dev/tty1 2>&1
where username is the name of your login user
...what this does is an autologin on tty1 [the 1st terminal]
then edit .bashrc on that user
and right at the end put something like :-
if [ $(tty) == /dev/tty1 ]; then
./script
fi

where script is the script you want to run.

so what happens is when the RPi is booted the 1st terminal will run your script - other terminals will want a login and if you login with your normal user will not auto run your script
if your script exits then you will be left at a command prompt, if you exit it will run again
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by grumpyoldgit » Fri Jun 01, 2012 11:41 am
Thanks for that; looks great. Just the sort of thing that people want to do. Candidate for Magpi3?
User avatar
Posts: 1458
Joined: Thu Jan 05, 2012 12:20 pm
by kernelcode » Sun Jun 03, 2012 11:43 am
I put stuff in /etc/rc.local if I want it to run on boot - it will run with root privileges (I think you could use sudo to stop that), and will run before login.
The advantage in my mind is that you aren't leaving a logged-in terminal (though maybe you could automatically log out with your method too?)
Posts: 9
Joined: Tue Nov 22, 2011 3:47 pm
by Joe Schmoe » Sun Jun 03, 2012 11:54 am
It's funny how that goes - one man's meat is another man's poison.

The *benefit* of doing it this way is that it runs visibly, meaning that you can see it running right there in the virtual console - and this is critical if it is interactive.

It is also true that when you put it into the /etc/rc* sequence, if it fails, it gums up your booting process (and this can be fatal). You have to be careful what you put there. Human frailty and all 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: 2269
Joined: Sun Jan 15, 2012 1:11 pm
by kernelcode » Sun Jun 03, 2012 11:58 am
Joe Schmoe wrote:It is also true that when you put it into the /etc/rc* sequence, if it fails, it gums up your booting process (and this can be fatal). You have to be careful what you put there. Human frailty and all that...

I didn't know this - why is that and what kind of issues are we talking about?
Posts: 9
Joined: Tue Nov 22, 2011 3:47 pm
by RaTTuS » Fri Jun 08, 2012 9:45 am
rc.local is good for starting extra services
doing it this way will give you an output to the display [and is what I need] and also the ability to stop the script drop to a terminal and restart it with ease .
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by Joe Schmoe » Fri Jun 08, 2012 9:54 am
RaTTuS wrote:rc.local is good for starting extra services
doing it this way will give you an output to the display [and is what I need] and also the ability to stop the script drop to a terminal and restart it with ease .


You're getting it. Good show.
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: 2269
Joined: Sun Jan 15, 2012 1:11 pm
by bredman » Fri Jun 08, 2012 10:02 am
These are two solutions for two very different things.

Add something to /etc/rc.local if you want it to execute every time the RPi is booted or rebooted.

Add something to ~/.bashrc if you want it to execute every time a specific user logs in.

On a RPi, you could argue that they are almost the same if there is likely to ever be only one user. It really depends on whether you want the command to be executed before or after the user enters his username/password.

@kernelcode
Putting something into /etc/rc.local adds it to the boot sequence. The word "sequence" is important, if your code gets stuck then the boot sequence cannot proceed. The same with ~/.bashrc, if you put a faulty command in here you may not be able to login.

Note that the RPi may have a file on the FAT partition named /boot/boot.rc which performs the same function as /etc/rc.local. This is safer because you can edit this partition from any computer.
Posts: 1413
Joined: Tue Jan 17, 2012 2:38 pm
by paulsmithenator » Tue Mar 05, 2013 7:50 pm
What's the difference between .bash_profile and .bashrc?
Posts: 6
Joined: Tue Jan 01, 2013 11:23 pm
by SuperTaranta » Mon Mar 11, 2013 5:54 am
Curious if this would work for commands?
So for instance running quake on login.
would remove the ./script and place sudo /home/user/quake3/ioquake.arm?
Is there a way to make it run once? so like it runs, but when you quit it, it doesn't attempt to again?

As well as Curious about the answer to the post above me as well :D
Posts: 12
Joined: Sat Mar 02, 2013 7:19 am
by RaTTuS » Mon Mar 11, 2013 9:00 am
paulsmithenator wrote:What's the difference between .bash_profile and .bashrc?

.bashrc is for non login shells and
.bash_profile is for login shells

so if you login then you .bashrc is run
if you open another terminal then .bash_profile is run

IIRC
man pages and google may help on this

SuperTaranta wrote:Curious if this would work for commands?
So for instance running quake on login.
would remove the ./script and place sudo /home/user/quake3/ioquake.arm?
Is there a way to make it run once? so like it runs, but when you quit it, it doesn't attempt to again?
As well as Curious about the answer to the post above me as well :D


yes - though with sudo it will still ask for a password
if you do it how I described in the 1st post after the ./script has run then you will be left at the command prompt
if you then
exit
then it will re-log you and re-run ./script
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by SuperTaranta » Mon Mar 11, 2013 3:25 pm
RaTTuS wrote:
paulsmithenator wrote:What's the difference between .bash_profile and .bashrc?

.bashrc is for non login shells and
.bash_profile is for login shells

so if you login then you .bashrc is run
if you open another terminal then .bash_profile is run

IIRC
man pages and google may help on this

SuperTaranta wrote:Curious if this would work for commands?
So for instance running quake on login.
would remove the ./script and place sudo /home/user/quake3/ioquake.arm?
Is there a way to make it run once? so like it runs, but when you quit it, it doesn't attempt to again?
As well as Curious about the answer to the post above me as well :D


yes - though with sudo it will still ask for a password
if you do it how I described in the 1st post after the ./script has run then you will be left at the command prompt
if you then
exit
then it will re-log you and re-run ./script


Alright :), thanks for the reply man.
Posts: 12
Joined: Sat Mar 02, 2013 7:19 am
by DetlevSchm » Tue Mar 12, 2013 10:27 am
RaTTuS wrote:.bashrc is for non login shells and
.bash_profile is for login shells

so if you login then you .bashrc is run
if you open another terminal then .bash_profile is run

You swapped the file names, it should read:

So, if you login, then your .bash_profile is run.
If you open another terminal, then .bashrc is run.

No harm done 8-).
User avatar
Posts: 65
Joined: Tue Mar 12, 2013 8:43 am
Location: Germany
by heldny » Wed May 01, 2013 3:55 pm
if i post my script at rc.local, the autoboot is ok.
the problem is that i use an audio library in the script.
Code: Select all
os.system('mpg123 mysound.mp3 &')

the raspberry don't play mysound.mp3 with the autoboot. where i can put my script that the script goes on after the library is up?
Posts: 4
Joined: Thu Apr 11, 2013 8:05 am
by RaTTuS » Wed May 01, 2013 4:01 pm
full paths to everything
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by heldny » Wed May 01, 2013 4:19 pm
in rc.local:

sudo python /home/pi/prog.py &

or where full path?
Posts: 4
Joined: Thu Apr 11, 2013 8:05 am
by RaTTuS » Thu May 02, 2013 10:32 am
in rc.local
you do not need
sudo
as it is run as root anyway
but the
os.system('mpg123 mysound.mp3 &')
may need to point mysound.mp3 to the full path of that
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by TheITGuy » Wed Feb 12, 2014 3:08 am
RaTTuS wrote:if you want to start a script off when your RPi boots up there are various ways but here is one that is very useful.
1) make sure your script runs [this is always useful ;-p]
2)edit /etc/inittab with your favourite root mode editor [I use sudo vi /etc/inittab YMMV]
2a) find the line
1:2345:respawn:/sbin/getty 115200 tty1
and replace it with
1:2345:respawn:/bin/login -f USERNAME tty1 </dev/tty1 >/dev/tty1 2>&1
where username is the name of your login user
...what this does is an autologin on tty1 [the 1st terminal]
then edit .bashrc on that user
and right at the end put something like :-
if [ $(tty) == /dev/tty1 ]; then
./script
fi

where script is the script you want to run.

so what happens is when the RPi is booted the 1st terminal will run your script - other terminals will want a login and if you login with your normal user will not auto run your script
if your script exits then you will be left at a command prompt, if you exit it will run again


Excellent Post!!
I've got a question hopefully someone can answer. I'm trying to set up a timelapse script to execute on bootup. I've finally got "raspiLapseCam.py" working, now I just need it to start upon boot up since this will be in an isolated location, plug & go setup.

This part states modifying the .bashrc,, and adding the following line.
if [ $(tty) == /dev/tty1 ]; then
./script

I just wanted to clarify the syntax before committing to this process.
I'm going to enter the following for this statement.
if [ $(tty) == /dev/tty1 ]; /usr/local/bin/raspiLapseCam.py

Is this correct???

Thanks for your time.
Posts: 2
Joined: Tue Feb 11, 2014 2:17 pm
by RaTTuS » Wed Feb 12, 2014 9:44 am
umm possibly I have to say I always
do
if [ ... ] ; then
command
fi

in bash scripts but IIRC the single line option is fine...
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX - Prosliver FTW
"That's not right, the badgers have moved the goalposts."
User avatar
Posts: 4166
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
by TheITGuy » Wed Feb 12, 2014 4:12 pm
RaTTuS wrote:umm possibly I have to say I always
do
if [ ... ] ; then
command
fi

in bash scripts but IIRC the single line option is fine...


So my syntax would be the following;
if [ $(tty) == /dev/tty1 ]; then /usr/local/bin/raspiLapseCam.py
fi

Have a good evening
Thanks RaTTuS
Posts: 2
Joined: Tue Feb 11, 2014 2:17 pm