The script below has the name notifyrssh.sh.
1: File=/home/vaios/Desktop/tempcron/liveusers.txt //OK
2: cd /home/vaios/Desktop/tempcron
3: ps -aux | grep sshd > liveusers.txt //OK file is being updated
4: // every minute
5: for name in gus tom max //***crontab-problem***
6: do
7: notify-send "$name" "DEBUG_MSG"
8: if grep -q $name "$File"; then
9: notify-send "$name" "ONLINE"
10: fi
11: done
The above script is being run in crontab. I open crontab list with :
crontab -e
And then i add the line :
* * * * * bash /home/vaios/Desktop/tempcron/notifyrssh.sh
Execution from shell prompt
The script seems to be executing fine when i run it regularly from the shell prompt. Being in the directory where the executable script is, i execute it like this :
./notifyrssh.sh
...and it works as i expect (updates the file liveusers.txt and gives me all the notification messages) . I again execute it like this :
bash /home/vaios/Desktop/tempcron/notifyrssh.sh
...and again the results are correct (update file liveusers.txt etc and gives me the notification messages).
Execution from crontab, every minute
The problem when the script is being executed every minute by crontab begins from line 5 and onwards until the end. The file liveusers.txt is being updated regularly every minute (I check this with : cat liveusers.txt) but then it doesn't give me the notifications i expect.
Let me explain what the above script actually does:
In the first 3 lines it updates the file liveusers.txt with the contents of the command :
ps aux | grep sshd
From line 5 onwards it checks a list of names (gus tom max) and then if it finds the name in the file liveusers.txt it gives me the notification message from line 9.
The notification message from line 7 is being put for debugging purposes exclusively. I expected that crontab script will give me at least the notofication from line 7 but that didn't happen also.
Any ideas?
#!/bin/bash
as the first line of your shell scripts. It is conceivable that the default shell that cron will use is something else. Also, I'd try using the full path tonotify-send
, just to be sure. – Ulrich Schwarz Jul 21 '13 at 18:17env
to your script and compare from inside and outside cron. – EightBitTony Jul 21 '13 at 19:52export DISPLAY=:0
, but you may need to experiment further to see what environment notify-send requires – glenn jackman Jul 21 '13 at 20:02