Show the results of jobs scheduled with cron in the terminal - linux

I've sucessfully configured a script that scans my system using cron in a scheduled hour of a day. However, I would like to see the results that it might have generated with its execution. Ex: rkhunter -c leaves logs in /var/log/rkhunter.log that I manually would have to check after it performed the task.
How can I configure cron to show a terminal with its results for example?
I've seen a similar case to mine, but it didn't work:
minute hour * * * DISPLAY=:0 xterm -e /dir_to_my_script/script.sh
But it gave the following error that I've not been able to solve so far:
Warning: This program is an suid-root program or is being run by the root user.
The full text of the error or warning message cannot be safely formatted
in this environment. You may get a more descriptive message by running the
program as a non-root user or by removing the suid bit on the executable.
xterm: Xt error: Can't open display: %s
Is there a way to accomplish it using cron as root? Thanks

Related

How to print the output of running program started by crontab job to a file on daily basis

A script start.sh is registered in crontab to run on daily basis
But I'd like to save the output of the program to a file, like cronlog-yyyyMMdd.log
I tried the following but failed:
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date+"\%F")-cron.log 2>&1
Anyone help me?
Using fully-qualified paths to all programs and files that are mentioned eliminate a whole class of errors from debugging issues with crontab entries. In this particular case, doing so would have only proved there were no problems with PATH issues in your crontab entry.
Your issue is that you need a space char between the date cmd and it's formatting argument string, instead of
$(date+"\%F")
#0 ^v
$(date +"\%F")
I commend you for your good crontab debug etiquette; capturing std-err and std-out to a file gives you 1. proof that your command ran when you expected it to (or if you have an error in your time specification, it will show you when the cmd did run). 2. you avoid having your localhost email box filling up with msgs from the cron daemon. 3 Any std-err msgs that were generated are also captured and are in generally in sync and time order with std-out messages.
IHTH.
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date +"\%F")-cron.log
Edit: as explained in comments, this answers the question by correcting a typo in the asker's attempted entry

Use linux command output in crontab

I made a shell script and registered to execute every 20 minutes.
Here is my crontab code.
*/20 * * * * sh /mypath/run_myprocess.sh &> /dev/pts/34
I editted code like this in order to see whether my process run correctly.
I get the result '/dev/pts/34' from tty command in terminal.
However, does anyone know how to use linux command results(in this case: /dev/pts/34)
in crontab? This is because I will use several terminal to run my tasks.
For example, in shell script, I can use linux command result in the form of $(command) such as
echo "$(date)"
directly.
Plus, if I type something on the terminal during process running with crontab, it actually gives result. For example,
Process is running........
ls
backup backup.sh Desktop Task_Folder shared_folder
[UserID] ~ #
So I guess cron jobs run correctly but in background.
Please help me to find out how can I bring cron jobs in foreground.
If you start a job on your console and background it you can then bring it to foreground. If the task is not yours or not started on your terminal then you can not.

Where to locate Centos 6 cron job .sh file

I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.

Cron job mysteriously stopped running?

I have a cron job on an Ubuntu 10.4 server that stopped running for no apparent reason. (The job ran for months and has not been changed.) I am not a *nix guru so I plead ignorance if this is a simple problem. I can't find any reason or indication why this job would have stopped. I've restarted the server without success. Here's the job:
# m h dom mon dow command
0 * * * * java -jar /home/mydir/myjar.jar >>/home/mydir/crontaboutput.txt
The last line in the output file shows that the program ran on 8/29/2012. Nothing after that.
Any ideas where to look?
There should be something in your system log when the job was run. The other thing you could >try is to add 2>&1 to the job to see any errors in your text file. – Lars Kotthoff yesterday
This proved to be the key piece of information - adding 2>&1 allowed me to capture an error that wasn't getting reported anywhere else. The completed command line then looked like:
java -jar /home/mydir/myjar.jar 2>&1 >>/home/mydir/crontaboutput.txt
Perhaps your cron daemon has stopped, or changed configuration (i.e. /etc/cron.deny). I suggest to make a shell script, and running it from crontab. I also suggest to run thru your crontab some other program (just for testing) at some other time. You can use the logger command in your shell script for syslog. Look into system log files.
Accepted answer is correct, (i.e, check the error logs) which pointed out the error in my case. Besides check for the following issues
include('../my_dir/my_file.php) may work from url but it will not work when cron job is run, will spit out error.
$_SERVER variables does not work inside cron os if you are using $_SERVER['DOCUMENT_ROOT'], it will not be recognized and you will have an error in the cron job.
Make sure to test the cron and have it run, send an email etc to make sure it is run.

How to test a cron job?

I'm using Ubuntu Linux 10.0.4. I want to run a script every 6 hours, every day. When I issue sudo crontab -e, I see:
# m h dom mon dow command
* 00,06,12,18 * * * /opt/scripts/selenium/run_nis_inf_tests.sh
However, I'm not seeing the expected outcome from my script, and I'm not even sure if its running. Is there a way to test, short of waiting until the specified time, that the script is running properly. Or, how can I view the errors the script is generating? - Dave
You can update the MAILTO variable to your email address, and cron should email you any STDOUT and STDERR output. Also check your syslog file /var/log/messages to see if the script is being executed by cron.
-Tony
Cron should mail it results so it looks like you have a problem.
Here, it seems you are missing a user to run the script as :
00,06,12,18 * * * user_name /opt/scripts/selenium/run_nis_inf_tests.sh
replace user_name by the name of the user the script needs to be run by, verify permissions of "run_nis_inf_tests.sh" and you should be ok.

Resources