Issue
I have a script:
/home/blah/test.sh
that contains the lines:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
netcat -v -w 5 169.254.123.123 xx >> /home/blah/output.txt
Where xx is the port number.
This script runs no problem from the command line, and the script runs when invoked via cronjob, but the output of netcat
is empty except for the "success" message output by the verbose flag to netcat
.
I have compared PATH variables and I have also explicitly tried SHELL=/bin/bash
in the crontab file before the cron entry. I have also obtained the env
details using * * * * * env > /home/blah/env.blah
and tried:
env - `cat /home/blah/env.blah` /home/blah/test.sh
This works, but I can not get the output from netcat to work within cronjob. A which netcat
returns /bin/netcat
so I've also tried replaceding netcat
in the script with /bin/netcat
and that also does not work.
I have tried it in crontab for user blah and for root -> for root both using sudo crontab -e
and alternatively editing /etc/crontab
(explicitly specifing the username as required). crontab entries tried:
*/5 * * * * /home/blah/test.sh>>/home/blah/log.out 2>>/home/blah/log.err
*/5 * * * * /home/blah/test.sh
*/5 * * * * /home/blah/test.sh>>/home/blah/log.out >>/home/blah/log.err 2>&1
And some others
Using Linux Mint 17.1 (Rebecca).
Any thoughts are appreciated.
Solution -d
flag is needed for netcat to run called from a cron job.
Solution
Answer (details are in the comment string to the original post):
The problem laid in the difference of invoking netcat
from the shell vs. invoking netcat
from a cronjob. Within cron there is not a shell instance, the solution to this is by invoking the -d
option which suppresses netcat
's need for a shell.
Thanks to all the commenters for their help!
Answered By - argh_bargle