Is it correct to use the rc.local file to start a program when the system starts?
It depends. If your program
- is a purely local modification and
- you don't need daemon control over the software (start/stop it later at system run) and
- a simple SIGTERM suffices to stop your program on system shutdown,
then yes, rc.local is just the point to put it.
If you intend to install the file on other systems, put a file in /etc/init.d and use the system specific commands to let it run at the appropriate runlevels.
Run script with rc.local: script works, but not at boot
I ended up with upstart, which works fine.
Why /etc/rc.local starts same Python script twice?
In this case; the line:
python3 /home/pi/myscript.py # <-- What is this?
is the python
command, as run by the sudo
command and the line:
sudo python3 /home/pi/myscript.py # <-- rc.local
is the sudo command, as invoked by rc.local
.
Using ps -fe
it also displays the parent pid of the processes, and from that it is easy to see that the python
command is a child of the sudo command (using a sudo bash
example):
$ ps -fe | grep bash
UID PID PPID C STIME TTY TIME CMD
0 15095 481 0 10:18am ttys000 0:00.06 sudo bash
0 15096 15095 0 10:18am ttys000 0:00.01 bash
so the parent of bash
is pid 15095
, which is the pid of the sudo
command that invoked bash.
Because rc.local
script is already run as root, the sudo
is not needed, so to avoid the situation of seeing apparently multiple copies, you can omit the sudo
in the script.
How to execute automatically command at rapsbian startup
The "official" way to run a program at boot time on systemd
-based Raspbian systems (and, in fact, most modern Linux distributions) is to create a systemd
unit file. There are specific instructions for Raspbian here:
https://www.raspberrypi.org/documentation/linux/usage/systemd.md
The entry After=network.target
is particular relevant in this case, because I imagine your program will need network interfaces to be up.
rc.local
is a hold-over from the SysV init days, and I've heard reports of it not working reliably in Raspbian. Creating a systemd
unit file provides a simple way to test the service using systemctl
without actually having to reboot. If it fails on boot, you'll probably need to use journalctl
to see the error messages.
What is the difference between /etc/rc.local and ~/.bashrc?
The difference is in when they are run and who they're running as when run i.e. rc.local is run on a change of run level and it runs as root. bashrc is bash specific and run on a non login shell as a particular user.
You can find a good explanation of rc.local
here
The script /etc/rc.local is for use by the system administrator. It is
traditionally executed after all the normal system services are
started, at the end of the process of switching to a multiuser
runlevel. You might use it to start a custom service, for example a
server that's installed in /usr/local. Most installations don't need
/etc/rc.local, it's provided for the minority of cases where it's
needed.
and you can find what you need about bashrc
man bash
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that
file exists. This may be inhibited by using the --norc option.
The --rcfile file option will force bash to read and
execute commands from file instead of ~/.bashrc.
There's more info on bashrc in this question...
https://superuser.com/questions/49289/what-is-the-bashrc-file
How to run a shell script at startup
The file you put in /etc/init.d/
have to be set to executable with:
chmod +x /etc/init.d/start_my_app
As pointed out by @meetamit, if it still does not run you might have to create a symbolic link to the file in /etc/rc.d/
ln -s /etc/init.d/start_my_app /etc/rc.d/
Please note that on the latest versions of Debian, this will not work as your script will have to be LSB compliant (provide at least the following actions: start, stop, restart, force-reload, and status):
https://wiki.debian.org/LSBInitScripts
As a note, you should always use the absolute path to files in your scripts instead of the relative one, it may solve unexpected issues:
/var/myscripts/start_my_app
Finally, make sure that you included the shebang on top of the file:
#!/bin/sh
using rc.local to start a jar file at startup is not working
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Related Topics
How to Dynamic Load The Library with Same Name But in Different Directory in Linux
Downgrade R Version and R Package Bioconductor
Windows .Crl to .Pem for Nginx
How to Use Sed to Replace a String in a File with a Shell Variable
What Is The Differences and Relationships Between "Process", "Threads", "Task" and "Jobs" in Linux
Jmeter:Difference Between Jmeter.Sh and Jmeter Without Extension File in Jmeter
How to Use Unicode in Aspell Dictionary
How to Implement Highly Accurate Timers in Linux Userspace
Udp Server Giving Segmentation Fault
How The Buffering Work in Socket on Linux
Undocumented Switches for 'Date' Command
Debian 7, Fstab, Mount.Cifs Invalid Argument
How to Check If a UId Exists in an Acl in Linux
Determining The Independent Cpu's (Specified with Affinity Id's) for Building Atlas