Question

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) ?

Était-ce utile?

La solution

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.

Autres conseils

Use sys.argv

import time
import sys

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

See here for a related discussion

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top