LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Crontab doesn't give the output of the file it runs in terminal (https://www.linuxquestions.org/questions/linux-software-2/crontab-doesnt-give-the-output-of-the-file-it-runs-in-terminal-727737/)

pranavojha 05-22-2009 03:44 AM

Crontab doesn't give the output of the file it runs in terminal
 
I have scheduled a shell script to run at a particular time of day using 'crontab'.
Code:

# crontab -l
30 22 * * 5 /bin/su root -c "/myscript.sh" 
#

However, the echo commands output are not displayed on the terminal window when the script executes. I tried setting the crontab in the following manner,
Code:

# crontab -l
30 22 * * 5 /bin/su root -c "/myscript.sh"> /dev/stdout 
#

.

But still I do not see the output on the screen. How can get the output of a script run by 'cron' on terminal screen ?

repo 05-23-2009 11:50 AM

Cron normally sends you mail if a cron task generated any output (this
should include error messages).
You could redirect the output to a logfile.

colucix 05-23-2009 12:11 PM

To get the output in a terminal, you have to explicitly open it from the cron job and launch the script as a terminal command. Using KDE I can do something like:
Code:

10 19 * * * env DISPLAY=:0.0 konsole --noclose -e echo hello world
note that you have to set the DISPLAY environment variable, otherwise cron does not know where to open a new terminal. Furtehrmore the user to which the crontab belongs, must have an X session open (take in mind you cannot connect to the X display of another user, since usually you don't have the right permissions).

Finally, you have to find a way to keep the terminal open after the command execution. In konsole, as you can see from my example, there is a --noclose option. You have to find the trick if you use another terminal.

kiwi9 08-24-2013 04:19 PM

colucix,

Thanks for that answer. An update for any debian users (I'm using Mint15 but Ubuntu etc should be the same).

I am using rdiffbackup so call a script that drives it. My command line is:

Code:

04 07 * * * env DISPLAY=:0.0 gnome-terminal -e /home/of user/.rdiffbackup

With gnome-terminal you can choose behaviour of the terminal when the command finishes. In my case I wanted the terminal to disappear if all backups complete successfully so I didn't. Instead I use the read command after an error to pause the terminal, awaiting user input.

A note for any rdiffbackup users having issues with cron. Cron has a very limited user environment compared with your terminal so you may have to fill in some missing variables for the SSH to work for rdiffbackup. In my case I needed to fill in SSH_AGENT_PID and SSH_AUTH_SOCK - the code is for the fish shell ... substituted " for the brackets around the command if you're using bash. The second assumes that the folder *keyring-* and associated file *ssh* is within /run - if its not then find out where your distro hides it and change it (the originator of this idea was searching /tmp for it).

Code:

set -x SSH_AGENT_PID (ps axww | grep -v grep | grep ssh-agent | grep -o -e [0-9][0-9][0-9][0-9])

set -x SSH_AUTH_SOCK (find /run/ -path '*keyring-*' -name '*ssh*' -print 2>/dev/null)

I include this in an old thread because google only cares about hit counts :)


All times are GMT -5. The time now is 04:51 AM.