how to add a timestamp along with the error log for a script from crontab - linux

I have a crontab running like:
*/15 * * * 4,5 /apps/ins/sid/compare_stats 2>> /apps/ins/sid/compare_stats.err
Everything working as expected. the only thing is I want my error logs to generate in the compare_stats.err file like this:
Jul 3 14:45:04 <error text>
which means I just want to add a date along with this. Is there any way to do it by modifying the crontab entry ( without really making any change in my script) ?
Thanks in advance.

Use the ts command which is part of the moreutils package. E.g.:
*/15 * * * 4,5 /apps/ins/sid/compare_stats | ts '[%Y-%m-%d %H:%M:%S]' 2>> /apps/ins/sid/compare_stats.err
This will prepend the timestamp to every line of the output and save it into your log.

Related

Cronjob to add datestamp to file not running

Good day everyone.
I have an issue, and Googling the issue has not helped me, basically I have the following requirement.
cronjob that runs 1st script, output is written to a file
file that is created, to have a date stamp
2nd script executes, mail the generated file as an attachment
The issue is with adding the timestamp, if I set the cron to run and just create a file with a generic filename the cronjob runs fine.
I have tried the following:
0 8-17/1 * * * python /usr/local/bin/script1.py >> /usr/local/bin/file_`date +\%Y-%m-%d`.txt 2>&1 && python /usr/local/bin/email_script.py
0 8-17/1 * * * python /usr/local/bin/acme_transcoding_check.py >> /usr/local/bin/file_$(date +"%Y-%m-%d").txt 2>&1 && python /usr/local/bin/email_script.py
Server is running Ubuntu 16.04
You need to escape the percent-sign (%) with a backslash as explained in this answer (not mine).

Crontab cannot execute

I tried to use crontab to execute my py file everyday, but it can only create empty log file
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py > /Users/UserName/Downloads/log.log
Then I tried to use SHELL file to execute a simple demand, if I put log file settings inside .sh file, no log file was created. Similarly, the crontab did not execute when I put python3 demand inside the SHELL file.
echo 1 > /Users/UserName/Downloads/new_log.log > /Users/UserName/Downloads/log.log
But if I directly run echo in Crontab, it can work out perfectly.
* * * * * echo 1 > /Users/UserName/Downloads/new_log.log
Does anyone know why this is happening? Thank you so much.
Try it with >>:
0 8 * * * /usr/local/bin/python3 /Users/UserName/Downloads/Crawling_1.py >> /Users/UserName/Downloads/log.log
then with >> it will create a file if it doesn't exist. If the file existe, it will be appended to the end of the file.
With > the whole file will replace it, if the file exist. If the file not exist*, nothing happens.
You can do the command also with >, but be sure, that the .log-file, where you will write inside, exist!

Using a crontab to gzip a file

I need to make a crontab to gzip a file named mh located on my desktop every 2 minutes in the same path. I tried
2 * * * * gzip home/Desktop/mh >> home/Desktop
But it is not working, any help is greatly appreciated.
There are several errors here.
The gzip command should be simply gzip home/Desktop/mh. Remove the >> and everything afterwards.
Your current cron will only run on the second minute of every hour. Instead you want */30 * * * * ... to run 30 times per hour.
Note that gzip is "destructive" in the sense that your original file (mh) will disappear after each gzip. That would be bad if some other process is trying to write to it continually...
If you want to keep the content of mh and just update mh.gz from it periodically, you want to do
*/30 * * * * gzip < /home/Desktop/mh > /home/Desktop/mh.gz

Append current date to the filename via Cron?

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.
My Cron job looks like this
mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql
But the file I get is this: db_backup+date no file extension or date.
I've also tried this command
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql
but that doesn't even give an file output.
What is the right syntax for getting the date appended to my file??
* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt
You just need to escape the percent signs.
Other date formats:
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
You should use `` instead of '' around the command you want to execute to generate the current date for your filename.
You must escape the format and use evaluation
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_`date +\%d\%m\%y`.sql
I need to create a new log file every time command is executed. So every day I should have a log like this /home/me/Logs/power_20151230.log
The crontab line that I use is this:
00 8 * * * /home/me/power.py ON >> /home/me/Logs/power\_`date +20\%y\%m\%d`
Note the underscore character must be escaped too.

Creating a Named Cron Job

How do you create a cron job from the command line, so that it shows up with a name in gnome-schedule?
I know how to create a cron job using crontab. However, all my jobs show up with a blank name. I'd like to better document my jobs so I can easily identify them in gnome-schedule, or similar cron wrapper.
Well, just made a cronjob in Scheduler, and took a look at my crontab file, and it looked like this:
0 0 * * * ls >/dev/null 2>&1 # JOB_ID_1
Notice the JOB_ID_1 at the end.
I went into ~/.gnome/gnome-scheduler/, looked at the files there, and there was one named just 1 (as in the number "one") which had a bit of info, including the name
ver=3
title=Hello
desc=
nooutput=1
So, I made a second cronjob:
0 0 * * * ls -al >/dev/null 2>&1 # JOB_ID_2
Copied the file 1 to 2 to match the JOB_ID_2, changed the description, making the file as:
ver=3
title=This is a test
desc=
nooutput=1
Then I switched over to Gnome-Schedule, and it had added the cronjob, and had the name updated.
Follow the same steps, and you should be able to manually name any cronjob you want

Resources