Perlscript runs fine when executed manually but not under cron

5,429

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:

  1. Use the full path for any calls made in the script.
  2. In the crontab entry, execute the script explicitly using perl.

    /usr/bin/perl /home/joe/netcheck.pl

  3. 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 &

  4. Temporarily replace exec "nm-applet" with exec "ls" or some other simple command to check that the problem is with the environment nm-applet expects, not with the script itself.

  5. Check if executing nm-applet –sm-disable helps.
  6. 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.

Share:
5,429

Related videos on Youtube

zedoo
Author by

zedoo

Updated on September 17, 2022

Comments

  • zedoo
    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
      Richie Marquez over 14 years
      #!/us/bin/perl?
    • Telemachus
      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
    zedoo over 14 years
    I changed the exec argument to an absolute path. Doesn't change anything.
  • zedoo
    zedoo over 14 years
    Has no effect.
  • David Mackintosh
    David Mackintosh over 14 years
    You might also have to do some tedious mucking about with xauth too.
  • zedoo
    zedoo over 14 years
    This sounds reasonable. I guess the real problem here is that my networking environment is managed by a desktop application..
  • zedoo
    zedoo over 14 years
    nice, i'll check out wicd.
  • zedoo
    zedoo over 14 years
    installed wicd, seems very slick.