How to run a python.py script from another python script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gintrado
    New Member
    • Mar 2010
    • 2

    How to run a python.py script from another python script

    I am very new to Python I'm trying to run a python script from within another python script. I can run the script from a unix command line by typing: nohup python script.py password > /tmp/outfile.log 2>&1 & Or, I can put that command in a shell script and run it by typing: ./script.sh Both work great. However I don't know how to put either the command or the shell script in a python.py script and get it to run from there.

    Let me know if I need to provide more information.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    os.popen() and subprocess.Pope n() should do what you need, or you could try importing the second script at required location in the first script.

    Comment

    • gintrado
      New Member
      • Mar 2010
      • 2

      #3
      Issue with crontab running a python script within a python script

      bvdet, thanks for the help. I think I found the problem and it is with crontab and python. I have a python script called scanner.py which I start from the command line and it runs all the time.
      Command: nohup python /home/dev/scanner.py password > /tmp/file.log 2>&1 &

      Occassionally, the process stops. For that I have another script called check.py which when run checks to see if the scanner.py is running and if not then run the command to start scanner.py, creates a log file and send notification scanner.py is down. When I run this script from the command line it also works.
      Command: python /home/dev/check.py > /tmp/check.log

      The problem enters when I try to schedule check.py to run every 5 minutes using crontab. Crontab will run every five minutes, a log file is created, I'm notified when the process is down, however, it won't start the scanner.py script like it does when run from command line.
      In crontab: */5 * * * * `python /home/dev/check.py > /tmp/check.log 2>&1`

      Also, all the scripts have access set to 755.

      Comment

      Working...