Perlscript läuft gut, wenn es manuell ausgeführt wird, jedoch nicht unter cron

5522
zedoo

mein WLAN-Setup schlägt mehrmals am Tag fehl, ein Neustart des Netzwerkmanagers von gnome hilft. Ich möchte dies automatisieren und folgendes Perlscript hacken:

#!/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"; } 

Mein erster Test bestand darin, das nm-Applet einfach von Hand zu beenden und das Skript manuell auszuführen. Es erkennt keine Konnektivität und verwandelt sich genau wie beabsichtigt in nm-Applet.

Nun derselbe Test, aber von folgendem Cron-Job ausgeführt:

*/1 * * * * /home/joe/netcheck.pl >> /home/joe/netcheck.log & 

Die Ausgabe in netcheck.log ist nur "nm-applet wird gestartet ...", aber es wird nicht gestartet. Der Prozess stirbt einfach sofort.

Jede Hilfe oder möglicherweise andere Lösung wird geschätzt.

0
#! / us / bin / perl? Richie Marquez vor 14 Jahren 0
@Rich: Gutes Auge, aber ich gehe davon aus, dass wenn das Skript so weit geht wie "nm-applet starten ...". das ist ein Tippfehler. Telemachus vor 14 Jahren 0

4 Antworten auf die Frage

2
nagul

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.

Schön, ich werde Wicd überprüfen. zedoo vor 14 Jahren 0
Wicd installiert, scheint sehr glatt. zedoo vor 14 Jahren 0
1
Telemachus

Cron läuft unter einer sehr minimalen Umgebung. Geben Sie explizite, vollständige Pfade für alle Shellbefehle an (z. B. /sbin/pinganstelle von usw.), und pingüberprüfen Sie, wo die relevanten Elemente zuerst stehen whereis pingusw., und es wird wahrscheinlich einwandfrei funktionieren.

Hat keine Wirkung zedoo vor 14 Jahren 0
1
bmb

Im Allgemeinen können Sie GUI-Apps nicht von cron aus starten, da cron keine Umgebung, Desktop, Anzeige usw. hat.

Versuchen Sie dies in cron

*/1 * * * * export DISPLAY=:0 && /home/joe/netcheck.pl >> /home/joe/netcheck.log & 

oder anstatt DISPLAY in der crontab zu setzen, versuchen Sie es im Skript selbst einzustellen. Ich bin nicht sicher, welcher Weg funktionieren wird.

Möglicherweise müssen Sie auch etwas langweiliges mit xauth herumspielen. David Mackintosh vor 14 Jahren 0
Das hört sich vernünftig an. Ich denke, das eigentliche Problem hier ist, dass meine Netzwerkumgebung von einer Desktop-Anwendung verwaltet wird. zedoo vor 14 Jahren 0
0
EmmEff

Versuchen Sie in Ihrem Skript, den Systempfad vor dem Aufruf von "exec nm-applet" zu sichern. nm-applet ist auf meinem System in / usr / bin vorhanden, und ich kann mir nicht vorstellen, dass der Standardpfad PATH nicht / usr / bin enthält.

Ich habe das exec-Argument in einen absoluten Pfad geändert. Ändert nichts. zedoo vor 14 Jahren 0