Perlscript runs fine when executed manually but not under cron
Solution 1
As everyone who answered has already pointed out, cron runs commands in a very minimal environment. I'd suggest you try this in sequence:
- Use the full path for any calls made in the script.
-
In the crontab entry, execute the script explicitly using perl.
/usr/bin/perl /home/joe/netcheck.pl
-
Capture both stdout and stderr output of the script.
/usr/bin/perl /home/joe/netcheck.pl 1>/home/joe/netcheck-stdout.log 2>/home/joe/netcheck-stderr.log &
Temporarily replace
exec "nm-applet"
withexec "ls"
or some other simple command to check that the problem is with the environment nm-applet expects, not with the script itself.- Check if executing
nm-applet –sm-disable
helps. - If you're still stuck, execute
strace nm-applet
instead to trace the system calls. Run this normally and within cron to identify the call from which the logs diverge. Debug from that point.
Having said this, I'm not surprised to see nm-applet failing to run properly from within cron. It probably needs access to the display and gnome libraries that are missing from within the cron environment. An at job might be better, but even that isn't ideal. I'd recommand using wicd instead if you need to reconnect from a cron job.
Solution 2
Cron runs under a very minimal environment. Supply explicit, full paths for all shell commands (e.g., /sbin/ping
instead of ping
and so on - check where the relevant items are first with whereis ping
and so on), and it will likely run fine.
Solution 3
Generally speaking, you can't start GUI apps from cron, since cron has no environment, desktop, display, etc.
Try this in cron
*/1 * * * * export DISPLAY=:0 && /home/joe/netcheck.pl >> /home/joe/netcheck.log &
or instead of setting DISPLAY in the crontab, try setting it in the script itself. I'm not sure which way will work.
Related videos on Youtube
zedoo
Updated on September 17, 2022Comments
-
zedoo over 1 year
my wireless setup fails several times a day, restarting gnome's network manager helps. I want to automate this and hacked the following perlscript:
#!/usr/bin/perl use strict; use warnings; my $result = system "ping -c1 -W1 192.168.1.1"; if ($result != 0) { print "No connectivity. Action required...\n"; my $pid = `pgrep nm-applet`; if ($pid) { print "Killing current nm-applet instance $pid\n"; system "kill $pid"; } print "Starting nm-applet..."; exec "nm-applet" or die "couldn't start nm-applet"; } else { print "Looks all fine. No action required\n"; }
My first test was to just kill nm-applet by hand and running the script manually. It detects no connectivity and just "morphs" into nm-applet, just as intended.
Now the same test but executed by the following cron job:
*/1 * * * * /home/joe/netcheck.pl >> /home/joe/netcheck.log &
The output in netcheck.log is just "Starting nm-applet..." but it doesn't get started. The process just dies immediately.
Any help or possibly other solution appreciated.
-
Richie Marquez over 14 years#!/us/bin/perl?
-
Telemachus over 14 years@Rich: Good eye, but I assume that if the script gets as far as "Starting nm-applet...". that's a typo.
-
-
zedoo over 14 yearsI changed the exec argument to an absolute path. Doesn't change anything.
-
zedoo over 14 yearsHas no effect.
-
David Mackintosh over 14 yearsYou might also have to do some tedious mucking about with xauth too.
-
zedoo over 14 yearsThis sounds reasonable. I guess the real problem here is that my networking environment is managed by a desktop application..
-
zedoo over 14 yearsnice, i'll check out wicd.
-
zedoo over 14 yearsinstalled wicd, seems very slick.