Run Python Script on OS boot
Solution 1
Place the script into /etc/rc.local
. Scripts there run as root when the system starts. It is also suitable for Raspberry Pi, as you specified in the comments.
In your case you want to run it as python /path/to/script.py &
Here's my sample rc.local
file, I use the same approach to run battery and temperature monitoring scripts
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/xieerqi/bin/batmon.sh &
/home/xieerqi/bin/preventShutdown.sh &
/home/xieerqi/bin/sh/temperature.sh &
Additional info on /etc/rc.local
Solution 2
You can also do this with cron by adding the following to your crontab:
@reboot username python /python/to/file.py
You may or may not need the username in there.
[EDIT]
There are two caveats to this approach:
- the
cron
daemon must be running (which is the case under normal circumstances); - the script or the crontab file must include the environment variables (if any) that will be needed.
Related videos on Youtube
Sijan Shrestha
Updated on September 18, 2022Comments
-
Sijan Shrestha over 1 year
This might be a question that has been repeated but i cannot find a correct way to do it .
What is my case ? ->I want to run a Python script.
What does the python script do in brief? ->The python script runs to setup a virtual private network.
How do I manually start it currently ? ->i use ./file.py start (this works)
what is not working ? ->when i try to start in the beggining (boot ) it doesnot do anything. I am using cron job which is not working at all, and i dont want to use it , i want to use the /etc/init.d/ .
This is my file.conf in /etc/init/xxx.conf
description "file start script" author "sijan <[email protected]>" exec python file.py start exec sleep 10 exec ifconfig ip0 11.0.2.251 exec ip=`ifconfig ip0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'` exec echo $ip >>/tmp/ip.log
I have added the python file in /etc/init.d/file.py
I am possibly not following the correct procedure to do this since i a very new to system level, however any idea on how to proceed is highly appreciated. I am very keen to get this fixed and learn
-
Jacob Vlijm over 7 yearsIt looks to me you want to run it on log-in, a job for Startup Applications.
-
Sergiy Kolodyazhnyy over 7 yearsDepends on what python file does. If it neess GUI, run it via startup applications. if it doesn't, run via /etc/rc.local
-
Sijan Shrestha over 7 yearsHi Serg, I am using A raspberry pi. The file is simple , it just setsup a virtual network . it does not need a GUI. I am having trouble figuring out the steps to correctly run in startup when system restarts if i put the file in /etc/init.d/file
-
Jacob Vlijm over 7 years...Not only gui defines the time&place. Is it a personal virtual network?
-
Sijan Shrestha over 7 yearsYes it is a personal virtual network. If I run the script manually it works fine. But having trouble with automation.
-
Andrea Lazzarotto over 7 yearsDon't put scripts in
/etc/init.d
. Add a line in/etc/rc.local
. Or, alternatively, configure the OpenVPN system service. -
Jacob Vlijm over 7 yearsHi sidzan, updated my answer. Do you manage?
-
Jacob Vlijm over 7 yearsCould you @ ping me? else I miss the message :) You shouldn' t give * root* permission to run the script without password, but yourself. root already has :). Nothing to add to rc.local, since we don' t use that. simply store the script in
/usr/local/bin
-
-
Jacob Vlijm over 7 yearsIsn't he setting up the network as user? Not sure from the question, but it works when he runs once logged in as user.
-
Sijan Shrestha over 7 yearsfor running manually the script i do the following:
sudo su
andcd /root/
and./file.py start
So its just this command which works perfectly fine . -
Sergiy Kolodyazhnyy over 7 yearsIf he needs to provide username to setup VPN , that can be done inside the script. From
rc.local
he doesn't need to provide his Raspberry user account -
Sergiy Kolodyazhnyy over 7 years@sidzan ok so add
python /root/file.py start &
to/etc/rc.local
-
Francois over 3 yearsexactly what I was looking for! nice one!