Start a python script at startup automatically?
1) Don't use old "init.d" method. Use something modern. If you have Ubuntu 15.04 and higher, you can use Systemd to create daemon that will be started automatically at startup. If you have for example Ubuntu older than 15.04 - use Upstart.
For Systemd:
Create unit file in /lib/systemd/system/you_service_name.service
with the following content (as far as I can see your python script doesn't spawn new process while running, so Type
should be simple
. More info here):
[Unit]
Description=<your_service_name>
After=network.target network-online.target
[Service]
Type=simple
User=<required_user_name>
Group=<required_group_name>
Restart=always
ExecStartPre=/bin/mkdir -p /var/run/<your_service_name>
PIDFile=/var/run/<your_service_name>/service.pid
ExecStart=/path/to/python_executable /path/to/your/script.py
[Install]
WantedBy=multi-user.target
Save this file and reload systemd:
sudo systemctl daemon-reload
Then add your service to autostart:
sudo systemctl enable you_service_name.service
you should see that Systemd created required symlinks after enable
action.
Reboot and see if it's up and running (ps aux | grep python
or sudo systemctl status you_service_name.service
). If there is something weird - check Systemd journal:
sudo journalctl -xe
UPD:
To launch your python script in desired virtualenv, just use this expression in your service unit file:
ExecStart=/venv_home/path/to/python /venv_home/path/to/your/script.py
2) You can also use crontab
, but you need to specify full path for desired shell there, for example:
@reboot /bin/bash /path/to/script.sh
If you need additional help - just let me know here.
Autostart C application in Ubuntu before to login
Yeah, that document looks good. It talks about what I was going to say, use init scripts.
How to run Java application on startup of Ubuntu Linux
Ideally you should create a service wrapper for your java application and then make this service run on startup example here.
Usesudo update-rc.d mytestserv defaults
to run your service wrapper on startup on Ubuntu
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
Related Topics
How to Test If a Variable Is a Number in Bash
Glibc Scanf Segmentation Faults When Called from a Function That Doesn't Align Rsp
How to Shutdown a Spring Boot Application in a Correct Way
What Killed My Process and Why
What Does Set -E Mean in a Bash Script
How to Pass Command Output as Multiple Arguments to Another Command
Is There a Way For Non-Root Processes to Bind to "Privileged" Ports on Linux
Change the X-Frame-Options to Allow All Domains
Remove a Specific Character Using Awk or Sed
How to Simulate Just One Enter in Command Line After Executing a Jar File
Argument List Too Long Error For Rm, Cp, Mv Commands
How to Fix 'Sudo: No Tty Present and No Askpass Program Specified' Error
Format and Then Convert Txt to CSV Using Shell Script and Awk
Convert String to Hexadecimal on Command Line
Disable Randomization of Memory Addresses
How to Get the Bssid of Currently Connected Network Through Bash