By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

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

P: 2
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.
Mar 29 '10 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
os.popen() and subprocess.Popen() should do what you need, or you could try importing the second script at required location in the first script.
Mar 29 '10 #2

P: 2
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.
Apr 6 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.