User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Auto start a script

Fri Jun 01, 2012 11:34 am

for Jessie | systemd see viewtopic.php?f=29&t=7192&p=828947#p828947

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 ;) ]
2) edit /etc/inittab with your favourite root mode editor [I use sudo vi /etc/inittab YMMV]
2a) find the line

Code: Select all

1:2345:respawn:/sbin/getty 115200 tty1
and replace it with

Code: Select all

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

Code: Select all

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
Last edited by RaTTuS on Wed Oct 14, 2015 10:27 am, edited 4 times in total.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
grumpyoldgit
Posts: 1452
Joined: Thu Jan 05, 2012 12:20 pm

Re: Auto start a script

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?

kernelcode
Posts: 16
Joined: Tue Nov 22, 2011 3:47 pm

Re: Auto start a script

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?)

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

Re: Auto start a script

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...
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)

kernelcode
Posts: 16
Joined: Tue Nov 22, 2011 3:47 pm

Re: Auto start a script

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?

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

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 .
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: Auto start a script

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.
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)

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: Auto start a script

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.

paulsmithenator
Posts: 6
Joined: Tue Jan 01, 2013 11:23 pm

Re: Auto start a script

Tue Mar 05, 2013 7:50 pm

What's the difference between .bash_profile and .bashrc?

SuperTaranta
Posts: 12
Joined: Sat Mar 02, 2013 7:19 am

Re: Auto start a script

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

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

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
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

SuperTaranta
Posts: 12
Joined: Sat Mar 02, 2013 7:19 am

Re: Auto start a script

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.

User avatar
DetlevSchm
Posts: 70
Joined: Tue Mar 12, 2013 8:43 am
Location: 3rd planet

Re: Auto start a script

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-).

heldny
Posts: 4
Joined: Thu Apr 11, 2013 8:05 am

Re: Auto start a script

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?

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

Wed May 01, 2013 4:01 pm

full paths to everything
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

heldny
Posts: 4
Joined: Thu Apr 11, 2013 8:05 am

Re: Auto start a script

Wed May 01, 2013 4:19 pm

in rc.local:

sudo python /home/pi/prog.py &

or where full path?

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

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
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

TheITGuy
Posts: 2
Joined: Tue Feb 11, 2014 2:17 pm

Re: Auto start a script

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.

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

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...
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

TheITGuy
Posts: 2
Joined: Tue Feb 11, 2014 2:17 pm

Re: Auto start a script

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

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Auto start a script systemd [Jessie]

Wed Oct 14, 2015 10:26 am

If you have a script that you want to run on boot up the it's pretty easy in Jessie via systemd
assuming that you have created your script correctly i.e.
the first line has the interpreter stated :-
#!//bin/sh
or python or perl or whatever full path please
and has the correct modes i.e.
chmod 755 scriptname
i.e.
nano Ping

Code: Select all

#!/bin/sh
ping -c 50 www.google.com
chmod 755 Ping
./Ping #see that it works

now we create the service
sudo nano /lib/systemd/system/ping.service

Code: Select all

[Unit]
Description=Ping auto test

[Service]
ExecStart=/home/pi/Ping

[Install]
WantedBy=multi-user.target
then you can see its status like

Code: Select all

sudo systemctl status ping.service
enable it like

Code: Select all

sudo systemctl enable ping.service
run it like

Code: Select all

sudo systemctl start ping.service
you will not see anything on the screen this is to be expected
but if you

Code: Select all

sudo systemctl status ping.service
you can see it running
also you can see that it is running via

Code: Select all

ps axOT
this script will run when you reboot the machine when it enters the multi-user part of the boot

for my next trick I'll start up a script on a spare virtual terminal that can accept input see
viewtopic.php?f=29&t=7192&start=25#p898424
Last edited by RaTTuS on Thu Jun 08, 2017 7:09 am, edited 3 times in total.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: Auto start a script

Wed Oct 14, 2015 3:32 pm

for my next trick I'll start up a script on a spare virtual terminal that can accept input
But that's the whole point. That's what we want to do - run it on a VC.

All the stuff before that - in your post - is just noise, because it is essentially doing it the 20th Century way.

I know this sounds harsh, but you have to be aware of how people read. Their attention span dwindles fast after the first few sentences. Most people won't even notice that the substantive part of your post is in the last sentence.
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
tadd
Posts: 14
Joined: Wed Dec 17, 2014 5:15 am
Location: Raleigh, NC
Contact: Website

Re: Auto start a script systemd [Jessie]

Wed Oct 14, 2015 4:02 pm

RaTTuS wrote:then you can see its status like

Code: Select all

sudo systemctl status ping.service
enable it like

Code: Select all

sudo systemctl enable ping.service
run it like

Code: Select all

sudo systemctl start ping.service
you will not see anything on the screen this is to be expected
but if you

Code: Select all

sudo systemctl status ping.service
you can see it running
This works! Yeah. is there a concise and relevant text about the writing of a .service file that we can reference? I'm specifically interested in Raspberry PI JESSIE compatible operation. One feature I'm immediately looking for is the ability to respawn the executable if it quits.

Thanks for any help!
Tadd

User avatar
RaTTuS
Posts: 10108
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto start a script

Wed Oct 14, 2015 4:06 pm

yeah the systemd tutorial stuff ;)
if you have
Restart=always
in the [Service] section then it will restart

the next one will be a bit more involving - as it has to do a lot more but I'm not in a position to post it right now as I have some win10 PC issues I'm dealing with.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
tadd
Posts: 14
Joined: Wed Dec 17, 2014 5:15 am
Location: Raleigh, NC
Contact: Website

Re: Auto start a script

Wed Oct 14, 2015 4:59 pm

RaTTuS wrote:yeah the systemd tutorial stuff ;)
.
Thanks.
I also found this document
https://wiki.archlinux.org/index.php/Systemd

Tadd

Return to “Advanced users”