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


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


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:

  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.