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 <sijanshrestha2@gmail.com>"

exec python file.py start
exec sleep 10
exec ifconfig ip0

exec ip=`ifconfig ip0 | grep 'inet addr:'| grep -v '' | 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

  • 2
    It looks to me you want to run it on log-in, a job for Startup Applications. – Jacob Vlijm Aug 26 '16 at 7:46
  • Depends on what python file does. If it neess GUI, run it via startup applications. if it doesn't, run via /etc/rc.local – Sergiy Kolodyazhnyy Aug 26 '16 at 7:51
  • Hi 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 – Sijan Shrestha Aug 26 '16 at 7:57
  • ...Not only gui defines the time&place. Is it a personal virtual network? – Jacob Vlijm Aug 26 '16 at 8:15
  • 1
    Don't put scripts in /etc/init.d. Add a line in /etc/rc.local. Or, alternatively, configure the OpenVPN system service. – Andrea Lazzarotto Aug 26 '16 at 8:37

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

  • Isn't he setting up the network as user? Not sure from the question, but it works when he runs once logged in as user. – Jacob Vlijm Aug 26 '16 at 10:45
  • for running manually the script i do the following: sudo su and cd /root/ and ./file.py start So its just this command which works perfectly fine . – Sijan Shrestha Aug 26 '16 at 10:46
  • If 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 Aug 26 '16 at 10:47
  • 1
    @sidzan ok so add python /root/file.py start & to /etc/rc.local – Sergiy Kolodyazhnyy Aug 26 '16 at 10:47

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.


There are two caveats to this approach:

  1. the cron daemon must be running (which is the case under normal circumstances);
  2. the script or the crontab file must include the environment variables (if any) that will be needed.
  • exactly what I was looking for! nice one! – Francois 17 hours ago

It looks like you need to end the string quotation after the email address, otherwise the other code will be considered part of the author string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.