Passing arguments from sh script(remade from etc/init.d/skeleton) to python

1k views Asked by At

I have a script and I need him to pass arguments to python program. I take arguments:

DAEMON_ARGS=""
start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --startas $DAEMON    \
   $DAEMON_ARGS \                                                  
   || return 2

Where $DAEMON is path to my .py file. I need to pass some numeric arguments like this

sudo /etc/init.d/sleepdaemon start 10

And this number I must pass to sleep.py. The code of sleep.py:

#! env/bin python

import time
sleep(n)

How do I make n = 10(the argument that wass pass from console) ?

2

There are 2 answers

1
Andrei Boyanov On BEST ANSWER

First, add the arguments to your daemon invocation:

DAEMON_ARGS="start 10"

Second, consume them in your python program:

#! env/bin python

import sys
n = sys.argv[2]

import time
sleep(n)

Look here for more detailed information about command line arguments use in Python.

0
doctorlove On

Use sys.argv

import time
import sys

time.sleep(int(sys.argv[11])

See here for a related discussion