How to setup a crontab to execute at specific time - cron

How can I set up my crontab to execute X script at 11:59PM every day without emailing me or creating any logs?
Right now my crontab looks something like this
#daily /path/to/script.sh

When you do crontab -e, try this:
59 23 * * * /usr/sbin/myscript > /dev/null
That means: At 59 Minutes and 23 Hours on every day (*) on every month on every weekday, execute myscript.
See man crontab for some more info and examples.

Following up on svrist's answer, depending on your shell, the 2>&1 should go after > /dev/null or you will still see the output from stderr.
The following will silence both stdout and stderr:
59 23 * * * /usr/sbin/myscript > /dev/null 2>&1
The following silences stdout, but stderr will still appear (via stdout):
59 23 * * * /usr/sbin/myscript 2>&1 > /dev/null
The Advanced Bash Scripting Guide's chapter on IO redirection is a good reference--search for 2>&1 to see a couple of examples.

You will with the above response receive email with any text written to stderr. Some people redirect that away too, and make sure that the script writes a log instead.
... 2>&1 ....

Related

Crontab doesn't execute R script

I'm trying to setup a Cron job on my system by adding the following line
17 12 * * * Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1
yet, I cannot see the file the output is being written to. I've consulted the following answers, but to no avail:
Why did my crontab not trigger
CronJob not running
When I run the following command on the terminal:
Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1
I get the output file as expected. I also added the following line to crontab:
* * * * * echo hi > ~/output.txt 2>&1
and it works just fine. I'm not sure what is wrong with the first command. Any help would be appreciated. Thanks.
Try Below trick, Create one script script.sh like below -
cat script.sh
Rscript ~/path/to/file/script.R > ~/output_$(date +\%d\%m\%y).txt 2>&1
And then create below entry in crontab.
17 12 * * * /bin/bash /path/to/script.sh
I fixed this by replacing Rscript in my crontab with /usr/local/bin/Rscript (or wherever your Rscript is located - do which Rscript to find out).

How to redirect cron job output to stdout

I have a cron job and its output is now redirected into a file. It looks like the following
0 9 * * * /bin/sh /bin/cleanup.sh > /home/darkknight/cleanup.log
Can any one help me to rediect its output to stdout?
Running process has a PID and its fd (file descriptor) is mapping to /proc/<PID>/fd. And we can find PID of the running cron process at /var/run/crond.pid.
To send cron log to stdout, we could write log to fd number 1 of the process started by cron.
0 9 * * * /bin/sh /bin/cleanup.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
Enter tty on any terminal and we will get device file for that particular teminal window like /dev/pts/1. Redirct the cron job into this file as
cleanup.sh > /dev/pts/1
Run cat /home/darkknight/cleanup.log then you get the output on STDOUT.
If you can't see what you expect as output, maybe you need to modify the cron as following:
0 9 * * * /bin/sh /bin/cleanup.sh > /home/darkknight/cleanup.log 2>&1
To get what cleanup.sh writes on its STDERR.
If you don't want to lose the output of yesterday, modify as following:
0 9 * * * /bin/sh /bin/cleanup.sh >> /home/darkknight/cleanup.log 2>&1
Or, just execute /bin/sh /bin/cleanup.sh then you get both STDOUT and STDERR on your terminal.

Check logfile size and alert if not updated in 30 minutes

I believe this is possible to do but not being an expert on Linux and shell scripting.
I believe I can use the watcher command to check the logfile directory but I am not sure how I would set it up to send an email alert to advise that this logfile directory hasn't been updated in 30 minutes.
Perhaps crontab would be of use to you:
Perform an action every minute:
echo "* * * * * touch $(pwd)/washere1" | crontab
Perform an action every 30 minutes:
echo "30 * * * * touch $(pwd)/washere2" | crontab
Check for the existence of the file "washere1" or "washere2" to verify that the command worked.

Crontab command not working

I know this question has been asked many, many times and I've done a lot of research but still I'm not able to run this extremely simple cron:
$ crontab -l
* * * * * /bin/date
This should, ideally, print the date every minute.
There is no cron.allow or cron.deny file, and the cron daemon is working:
ps -e | grep cron
1119 ? 00:00:00 cron
17646 ? 00:00:00 cron
Any idea what might be wrong?
Cron processes run in a separate subprocess of their own, so the output of a cron job will not be visible to you in your shell.
Instead, you will have to capture the output of your cron commands and save them. So, set your cronjob like:
* * * * * /bin/date >> /home/user/date.log
And now, if you will tail this log file, you will start seeing the result.

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Resources