0

i'm trying to get informations of a network interface on a linux machine with a python script, i.e. 'ifconfig -a eht0'. So i'm using the following code:

import subprocess proc = subprocess.Popen('ifconfig -a eth0', shell=True, stdout=subprocess.PIPE) proc.wait() output = proc.communicate()[0] 

Well if I execute the script from terminal with

python myScript.py 

or with

python myScript.py & 

it works fine, but when it is run from background (launched by crontab) without an active shell, i cannot get the output.

Any idea ?

Thanks

5
  • 1
    What do you do with output to see if it worked or not (whether in background or in active shell)? Do you save it to a file? If so, does the file get created but with no content or no file at all? Commented Aug 1, 2017 at 8:27
  • Does it make a difference if you don't set shell=True, eg proc = subprocess.Popen(['ifconfig', '-a', 'eth0'], stdout=subprocess.PIPE) ? Commented Aug 1, 2017 at 8:29
  • the output is sent to a remote db, and when it is run from terminal i can read the data. While if i use shell=False i get this error OSError: [Errno 2] No such file or directory Commented Aug 1, 2017 at 8:34
  • If you don't use shell=True you must pass the command line as a list, as shown in my previous comment. Commented Aug 1, 2017 at 8:43
  • @PM2Ring i have used the list to pass the command and it raised this exception [Errno 2] No such file or directory Commented Aug 1, 2017 at 9:07

3 Answers 3

1

Have you tried to used "screen"?

proc = subprocess.Popen('screen ifconfig -a eth0', shell=True, stdout=subprocess.PIPE) 

I'm not sure that it can work or not.

Sign up to request clarification or add additional context in comments.

3 Comments

If you're not sure that it should be a comment, not an answer
SO Users with less than 50 reputation cannot comment on other people's posts.
Yes, I can not comment. I met the same issue before. the solution worked for me. But I am not sure that it works for others
0

Try proc.stdout.readline() instead of communicate, also stderr=subprocess.STDOUT in subprocess.Popen() might help. Please post the results.

3 Comments

Launching the script from terminal gives eth0 Link encap:Ethernet HWaddr 18:b2:23:a4:12:1e instead of the full output, and if run by cron it gives again empty string.
I suggest you to use proc.communicate then. Have you tried redirecting the standard error to output with 2>&1 ?
also, try invoking a script from crontab and redirecting the output at the line where you invoke the script (might be related, I'm not a strong crontab user link
0

I found a solution to the problem, i guess that the system is not able to recognize the function ifconfig when executed by the crontab. So adding the full path to the subprocess allows the script to be executed properly:

`proc = subprocess.Popen('/sbin/ifconfig -a eth0',shell=True,stdout=subprocess.PIPE) proc.wait() output = proc.communicate()[0]` 

and now i can manage the output string.

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.