Search code examples
linuxcron

How to send UDP packet from Debian 9 CRON?


I am trying with these 2 scripts:

test1,sh:

#!/bin/sh
NOW=$(date +"NOW;%Y;%m;%d;%H;%M;%S")
echo -n $NOW | nc -u -q 2 -w 2 192.168.0.252 4210

test2.sh:

#!/bin/sh
NOW=$(date +'NOW;%Y;%m;%d;%H;%M;%S')
echo -n $NOW > /dev/udp/192.168.0.252/4210

Both scripts works fine when directly execute on terminal, client successfully receiving the UDP packets.

*/5 * * * * root /etc/test/test1.sh
*/5 * * * * root /etc/test/test2.sh

But doesn't work when executing in CRON, client did not receive the UDP packets.

**sudo grep CRON /var/log/syslog**
Mar 10 16:40:01 localhost CRON[12281]: (CRON) info (No MTA installed, discarding output)
Mar 10 16:40:01 localhost CRON[12286]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Mar 10 16:40:01 localhost CRON[12287]: (root) CMD (root /etc/test/test1.sh)
Mar 10 16:40:01 localhost CRON[12289]: (root) CMD (root /etc/test/test2.sh)

Please help


Solution

  • Is the next to last block the output of crontab -l? Looks to me like there's an extra "root" in there.

    The hint is in your syslog output:

    Mar 10 16:40:01 localhost CRON[12289]: (root) CMD (root /etc/test/test2.sh)
    

    Remove the root from your crontab and you'll have better luck.