Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

System command from crontab

by adismaug (Acolyte)
on Dec 07, 2009 at 10:46 UTC ( #811467=perlquestion: print w/replies, xml ) Need Help??
adismaug has asked for the wisdom of the Perl Monks concerning the following question:

Dear perlmonks, I have a strange problem, when running a script from shell and redirecting the output the script function fine, when I add the script to a crontab job and try redirecting output to a file noting happens!
You help please.

crontab: /1 * * * * /root/scripts/perf/ > /tmp/test.txt 2>&1 #!/usr/local/bin/perl @route = qx { hping -S -c 3 -p 80 2>&1 }; chomp(@route); @route = grep(/\w+/g, @route); foreach $line (@route) { print "$line\n"; }

Replies are listed 'Best First'.
Re: System command from crontab
by cdarke (Prior) on Dec 07, 2009 at 11:41 UTC
    Check $? after the qx command, it might be returning an error. Cron might not be executing your environment files. You might find that the path is not setup correctly - it is safer to use the full path name for programs like hping rather than relying on the path in cron.
Re: System command from crontab
by Bloodnok (Vicar) on Dec 07, 2009 at 13:22 UTC
    I dare bet that hping is on your path when the script is run from CLI, but isn't when run by cron, try something like this in your crontab
    /1 * * * * . $HOME/.profile; /root/scripts/perf/ > /tmp/test.tx +t 2>&1
    On reflection, methinx there's also a typo - /1 isn't, AFAIK, valid crontab syntax.

    A user level that continues to overstate my experience :-))
Re: System command from crontab
by vitoco (Friar) on Dec 07, 2009 at 13:24 UTC

    Is the first field of the following crontab line ok?

    crontab: /1 * * * * /root/scripts/perf/ > /tmp/test.txt 2>&1

    I mean, "/1" should be "*/1", but that is the same as "*"!!!

    BTW, as it was said, not every environmental variables you can see in your terminal session are available from within crond deamon. Although cron command could save some of them for your script, you must set all your required environmental variables at the beginning of the script or crontab file.

Re: System command from crontab
by stefbv (Curate) on Dec 07, 2009 at 11:17 UTC
    I think the output is by default mailed to root.
      I think the output is by default mailed to root.

      That only happens if the command run by cron sends output to STDOUT or STDERR and they haven't been redirected somewhere else (/dev/null is a popular choice :-) because cron doesn't know what else to do with it. That's not the problem here at the OP is redirecting both handles to /tmp/test.txt.



        I was thinking that if for some reason the redirecting did not work, the error message mailed to the owner of crontab would contain some clues to solve the problem.

        Update: A reason like black magic maybe ;)

        Regards, Stefan

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://811467]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-05-21 11:45 GMT
Find Nodes?
    Voting Booth?