johnlwarence
Posts: 4
Joined: Mon Jul 30, 2012 8:47 pm

run a command on start up

Wed Aug 15, 2012 12:02 pm

Afternoon all,

I need to know how to run a command line on start up.

i am using my pi as a webcam server and the script runs from the command line, but i want this to start when i power up the pi, rather than logging in and typing out the script.

i am using uvccapture for software and wheezy for my distribution

please help

regards

john lawrence

User avatar
RaTTuS
Posts: 10514
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: run a command on start up

Wed Aug 15, 2012 12:11 pm

simple
put the command in /etc/rc.local
more advanced
put the command into /etc/init.d
and use different run levels
or
auto log yourself into a terminal and get the system to run a command depending on which terminal your are on....
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
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: run a command on start up

Wed Aug 15, 2012 12:12 pm

You could put the command at the end of the rc.local file in /etc/init.d/

I use this to run a python script.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

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

Re: run a command on start up

Wed Aug 15, 2012 12:15 pm

There are several ways to do this.

One easy way is to add your command to the file /etc/rc.local

To edit this file, use the command
sudo nano /etc/rc.local

When running a command during bootup, remember that you will be running as root and your normal environment will not be setup. This means that you must use the full path and name for each command, for example use "/bin/echo" instead of "echo".

To find the full path for each command, use the which command, example
which echo

Casey Scalf
Posts: 19
Joined: Wed Apr 15, 2015 5:31 pm

Re: run a command on start up

Sun Apr 19, 2015 12:42 am

I tried this but was unable to save the file because it said it could not be overwritten. How do I force this change?

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: run a command on start up

Sun Apr 19, 2015 1:30 am

Casey Scalf wrote:I tried this but was unable to save the file because it said it could not be overwritten. How do I force this change?
cd /etc
sudo vi rc.local
marcus
:ugeek:

Casey Scalf
Posts: 19
Joined: Wed Apr 15, 2015 5:31 pm

Re: run a command on start up

Sun Apr 19, 2015 1:34 am

Excellent!

Unfortunately I just tried to make a sweeping permissions change to the entire /etc folder and completely ruined everything... Don't do it kids, don't make sweeping permissions changes if you don't know what you are doing...

I will try that though. Does that mean it is a temporary permissions change? What should I expect? (Still waiting on the new image to mount on the RPi).

Casey Scalf
Posts: 19
Joined: Wed Apr 15, 2015 5:31 pm

Re: run a command on start up

Sun Apr 19, 2015 2:29 am

I tried this a bunch and it just doesn't load.

I am set to get into the GUI and I saw it run it when the commands were streaming past but nothing happens? I made sure to put it outside of the main loop as well.

When I find the script and "double-click" it after the Pi has booted up it will launch the program as it supposed to. Why won't it do that on start?

... Isn't there a purpose built way to do this? I feel like sticking it in a file is a little ad-hoc

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: run a command on start up

Sun Apr 19, 2015 4:16 am

Casey Scalf wrote: ... Isn't there a purpose built way to do this? I feel like sticking it in a file is a little ad-hoc
Not at all. You can also start things automatically by time of day by placing your scripts in crontab files.

There is a file on your RPi called /etc/crontab which holds entries that will run at various times of the hour, day, week, month/ you get the idea.

In fact, there is a directory called cron.d that can hold multiple files for various times of day, week, month... often I will put my automation programs and scripts there, rather than boot-up.

rc.local is the preferred way to launch additional startup codes at boot time... its a gnu/linux thing...
marcus
:ugeek:

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

Re: run a command on start up

Sun Apr 19, 2015 5:57 am

MarkHaysHarris777 wrote:
Casey Scalf wrote: ... Isn't there a purpose built way to do this? I feel like sticking it in a file is a little ad-hoc
rc.local is the preferred just one way to launch additional startup codes at boot time... its a gnu/linux thing...
FTFY ;)
Putting things in rc.local can be risky if you are not careful as a single mistake can result in a system that won't boot properly.

A safer alternative is to put the entries in the user's crontab file (don't change /etc/crontab, that one should be left alone).
Type crontab -e to edit your current crontab (or sudo crontab -e to edit the root crontab so commands don't need sudo), and add entries like this

Code: Select all

@reboot /home/pi/myscript >/tmp/myoutput 2>/tmp/myerrors
@reboot /usr/bin/python /home/pi/mypythonscript >/tmp/myoutputtoo 2>/tmp/myerrortoo
and they will run shortly after the system has booted.
You can check the files in /tmp for any messages from the scripts.

Casey Scalf
Posts: 19
Joined: Wed Apr 15, 2015 5:31 pm

Re: run a command on start up

Sun Apr 19, 2015 7:54 am

Great to know there are options!

I am looking for one that starts at the very beginning. This is for an art installation and needs to boot right away so anyone can just plug it in.

So even though I definitely will be incorporating those chronological ones I am looking for an approach that I triggered only once when the computer boots up.

Is it possible the current paradigm with rc.local is running the command before the program has a proper chance to load?

Casey Scalf
Posts: 19
Joined: Wed Apr 15, 2015 5:31 pm

Re: run a command on start up

Sun Apr 19, 2015 9:11 am

After trying a bit further I am able to get a script to be executed as rc.local is called. I can even run both of these, the script I wrote and rc.local file to produce the desired end command - this works fine.

It is when I try to get this to be ran fresh from startup that it gets tough.

It seems that there are two ways of running things from start. Startup into the console and startup into the GUI. After trying to add a delay and other things to run it after a few seconds in the GUI mode I tried to set it to run from the console startup mode: which of course you need to enable auto-login or what's the point of aiming for a script to be run in when the power is plugged in if you need to type something with a keyboard.

I followed a few guides, http://elinux.org/RPi_Debian_Auto_Login, and they all led to severe system failures in my case. It seemed very hazardous...

As I look back from a few good hours of trying this I can only imagine this must be a simple thing I am overlooking. People make these machines to launch all sorts of programs from the start and never need to be touched.

I am trying to launch Pi Presents https://github.com/KenT2/pipresents-next if that is of any help.

Here is the 1) code to be ran, 2) from a script, that is 3) called by rc.local on startup (I am fine with the console or GUI version as long as it just starts right up).

Code: Select all

sudo python /home/pi/pipresents/pipresents.py -o /home/pi -p Kiosk -g -f -b

kaksi
Posts: 101
Joined: Tue Mar 10, 2015 6:19 am

Re: run a command on start up

Tue Apr 21, 2015 9:51 am

Casey Scalf wrote:I followed a few guides, http://elinux.org/RPi_Debian_Auto_Login, and they all led to severe system failures in my case. It seemed very hazardous...
As I hate passwords, the very first modification I made to Raspbian was the first guide. It works perfectly for 3 months now.

Are you sure you did not make a typo?

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: run a command on start up

Tue Apr 21, 2015 5:51 pm

From the github link you provided I noticed it says:
"Pi Presents MUST use Raspbian and be run from the desktop (startx)."

If you try to start it from rc.local, or using @reboot in crontab you obviously won't have a desktop yet. You should try adding it to the autostart file for the LXDE desktop so it will start when the desktop starts.

You can put the startup in two different locations, depending on whether you want it to start for any user, or just for a specific user. For any user the file is located at:

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
Note that you may have a /LXDE/ folder rather than /LXDE-pi/ if so use that one. If you only want it to start up for a specific user put it in their home .config settings.

Code: Select all

/home/<username>/.config/lxsession/LXDE/autostart
If you want to start it up for all users open the file using sudo, since that file belongs to root.

Code: Select all

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
Add this last line to the autostart file

Code: Select all

@/usr/bin/python /home/pi/pipresents/pipresents.py -o /home/pi -p Kiosk -g -f -b
Then exit and save it. That should work for you.

SmokeU27
Posts: 4
Joined: Fri Nov 02, 2012 2:33 pm

Re: run a command on start up

Thu Apr 23, 2015 12:52 pm

I found Iceweasel and it works great on the web page that I am using, but I still can't get it to autostart and go to a web page.
Maybe I am not editing the right file for autostart...

This is the file I am editing...
sudo nano /etc/xdg/lxsession/LXDE/autostart

These are the updates I added

@xset s off
@xset -dpms
@xset s noblank
@iceweasel --kiosk http://"website"

I have also tried
@iceweasel-browser --kiosk http://website

Thanks

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: run a command on start up

Thu Apr 23, 2015 2:57 pm

I'd start simple to make sure it works, and Iceweasel starts up automatically then add to that.

Since you always want to start on the same page I'd open Iceweasel and set your desired page as the home page, then close Iceweasel. That eliminates the need to pass it a URL at startup.

Have you actually tried running it in kiosk mode, I think Iceweasel requires a plugin to do that.

You may have an /LXDE-pi/ folder, if so use that instead of /LXDE/. I have at least one that has both "/LXDE/autostart" and "/LXDE-pi/autostart" but it only works if I use the "/LXDE-pi/autostart".

With the Iceweasel homepage set just see if you can start it with

Code: Select all

@iceweasel
If that works then you can try adding kiosk mode.

zappfinger
Posts: 34
Joined: Tue Nov 26, 2013 7:25 pm

Re: run a command on start up

Thu Jan 19, 2017 5:23 pm

I put my vncserver command into rc.local, but it does not work:

vncserver :0 -geometry 1024x768 -depth 24

User avatar
DougieLawson
Posts: 36816
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: run a command on start up

Sun Jan 14, 2018 2:28 pm

hrostami wrote:
Sun Jan 14, 2018 2:24 pm

I made a video about this, a simple solution that works: https://youtu.be/Tvnrx-2QaUU
Why do you keep spamming this video across the forum when it's incomplete and incomprehensible?

The best way to get something running at startup is with a systemd service file as that doesn't need the GUI to be active. The best way to get something in the GUI active is with a simple autostart file or with a line in
/home/pi/.config/lxsession/LXDE-pi/autostart (prefixed with an at-sign).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12515
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: run a command on start up

Sun Jan 14, 2018 5:44 pm

hrostami wrote:
Sun Jan 14, 2018 2:24 pm
I made a video about this, a simple solution that works: https://youtu.be/Tvnrx-2QaUU
I wanted to delete 6 of Hrostami's 7 duplicate posts, I seem to have deleted all seven. Sorry.... :|

User avatar
RaTTuS
Posts: 10514
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: run a command on start up

Mon Jan 15, 2018 8:20 am

I change the advice on my 5 YO post , use a systemd .service file,
or autologin and use ~/.bashrc stuff [that needs some protection ]
or use LXDE autostart stuff for x based stuff
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

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: run a command on start up

Wed Jan 31, 2018 7:04 pm

zappfinger wrote:
Thu Jan 19, 2017 5:23 pm
I put my vncserver command into rc.local, but it does not work:

vncserver :0 -geometry 1024x768 -depth 24
use this:

Code: Select all

cd /etc
sudo nano rc.local

at the END of the script and BEFORE exit 0 command write this:

Code: Select all

su - pi -c  '/usr/bin/vncserver -geometry 1920x1080 -depth 24 ' 

periketi
Posts: 1
Joined: Wed May 09, 2018 4:56 am

Re: run a command on start up

Wed May 09, 2018 11:51 am

:D #! /bin/sh
# /etc/init.d/led_blink.sh

### BEGIN INIT INFO
# Provides: led_blink.sh
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start a program from boot
# Description: A simple script which will start a program from boot and stop upon shut-down
### END INIT INFO

# Put any commands you always want to run here.

case "$1" in
start)
echo "Starting example"
# run the program you want to start
/home/pi/led_shell_programming/a.out setup
/home/pi/led_shell_programming/a.out on

;;
stop)
echo "Stopping example"
# end the program you want to stop
/home/pi/led_shell_programming/a.out close
;;
*)wq
echo "Usage: /etc/init.d/example {start|stop}"
exit 1
;;
esac

Return to “Beginners”