Store cron log in timedate file - linux

I have this code currently:
echo "20 0 * * * cd /var/www/test/ && ./prog >> /var/log/program.log" >> mycron
This works fine, but now I want to store it in a a timedated file each time in the format like this:
program_YYYYMMDD_HHMMSS.log
Can anyone tell me how I can do this? I think I need to use the date variable but im not really sure how to implement it.

Yes, you are right. You can use the date variable.
echo "20 0 * * * cd /var/www/test/ && ./prog >> /var/log/program_$(date "+%Y%m%d_%H%M%S").log" >> mycron

Related

Crontab log directory?

Raspian Lite on Raspberry Pi 3:
I'm trying to output a crontab log with the following command:
* * * * * /home/pi/bin/pingTest.sh >> /home/pi/bin/pingLog/$( date +%Y )/$( date +%b )/l$( date +%d_%b ).log 2>&1
but in /home/pi/bin/pingLog/2019/Oct/ no l12_Oct.log file is generated. (Directory folders are automatically generated separately).
However if I try to manually run the command into console it works and the log file is correctly generated, so my guess is that crontab doesn't like my directory(?)
How to fix this problem? PLz help ;(
crontab command:
* * * * * /home/pi/bin/pingTest.sh >> /home/pi/bin/pingLog/$( date +%Y )/$( date +%b )/l$( date +%d_%b ).log 2>&1
console command:
home/pi/bin/pingTest.sh >> /home/pi/bin/pingLog/$( date +%Y )/$( date +%b )/l$( date +%d_%b ).log 2>&1
I expect an automatically generated log file into /home/pi/bin/pingLog/2019/Oct/ called l12_Oct.log
Problem fixed, it was a directory problem:
*/20 * * * * /home/pi/bin/pingTest.sh >> /home/pi/bin/pingLog/$( date +"\%Y" )/$( date +"\%b" )/$( date +"\%d" )/$( date +"\%d_\%b" ).txt 2>&1
Thanks for your time :)

Crontab setting - execute script every 55 minutes

I found a interesting thing during creation of my crontab setting.
I used this command:
crontab -e
and fill this line:
*/55 * * * * export DISPLAY=:0 && /home/user/Documents/script.sh $2>/dev/null
My idea was create scheduler, which start script.sh every 55 minutes.
But this script is execute in this times (for example):
08:55, 09:00, 09:05, 09:55, 10:00, 10:05, ...
and I don't know why.
Can someone explain me that?
Replace the script like this and it should work.
*/5 * * * * [ $(( $(date +%s) / 60 % 55 )) -eq 0 ] && export DISPLAY=:0 && /home/user/Documents/script.sh $2>/dev/null
minute-hour-day-month-year
* any value
, value list separator
- range of values
/ step values
Another option is a self-replicating 'at' job. Only advantage over cron is that it is less obvious, and also if you needed it to kick off not every X minutes, but X minutes after the last job completed. So your script will just contain a line to create a new 'at' job before it exits. Something like:
echo "/full/path/to/my/script > /root/myScript.at.log" | at now + X minutes
so every 5 minutes it will do this:
number of seconds elapsed since 1. 1. 1970 will divided by 60 = how many minutes
echo $(date +%s)
1476201056 ... second
echo $(( $(date +%s) / 60 ))
24603351 ... minutes
after that it will use modulo on count of minutes
When result of modulo is 0, it will send TRUE value.
And it is a typical logical AND
[ $((......)) -eq 0 ] && export DISPLAY.. && .../script.sh
Thank you.
It is really helpful :)

Running crontab only on one line in a file each time

I'm trying to configure crontab to execute at different times different lines of code inside a file. I basically have a bash script file that starts some java -jar. The problem is that each line should be executed at a different time. I can configure crontab to run the whole script at different times but no the lines to run. Now this is important that the bash file will stay only one file and not broken down to a few files.
Thanks!
One way of doing it (via command line arguments passed by cron)
some_script.sh:
if test $1 = 1 ; then
# echo "1 was entered"
java -jar some_file.jar
elif test $1 = 2 ; then
# echo "2 was entered"
java -jar another_file.jar
fi
crontab example:
* 1 * * * /bin/bash /home/username/some_script.sh 1
* 2 * * * /bin/bash /home/username/some_script.sh 2
Another approach (hour matching done in bash script)
some_script.sh:
hour=$(date +"%H");
if test $hour = 1 ; then
# echo "the hour is 1";
java -jar some_file.jar
elif test $hour = 2 ; then
# echo "the hour is 2";
java -jar another_file.jar
fi
crontab example:
* 1 * * * /bin/bash /home/username/some_script.sh
* 2 * * * /bin/bash /home/username/some_script.sh

Cronjob not running, attempting to echo string into file

My /etc/crontab looks like this:
* * * * * echo"Another 5 Minutes! " >> /tmp/5-minutes.txt
I figured this would append "Another 5 Minutes! " to /tmp/5-minutes.txt every minute. When I perform a 'less' command on the file, the changes are not there.
EDIT: If it helps, I have one other job in the crontab file, perhaps it's effecting the other job.
0 * * * * finger >> /tmp/hourly-finger.txt
* * * * * echo "Another 5 Minutes! " >> /tmp/5-minute.txt
Give a space after echo
echo "Another 5 Minutes! "
I figured it out. Because I'm editing the /etc/crontab directly I needed to specify a user for the crontab to run as. In my case I ran it as root so...
* * * * * root echo "Another 5 Minutes! : >> /tmp/5-minutes.txt

Creating a cronjob from a makefile?

I am trying to let my makefile setup a cronjob for my application. Unfortunately it appears to not be working as the $CRONENTRY variable seems to be empty. What am I doing wrong here?
addcron:
CRONENTRY="*/2 * * * * /usr/bin/node cronapp.js >> logfile.log"
crontab -l | { cat; echo ${CRONENTRY}; } | crontab -
Each command in a rule executes in its own subshell; variables do not survive from one command to the next. So if you want to use a variable this way, you have to string the commands together.
addcron:
CRONENTRY="whatever" ; \
do_something_with $(CRONENTRY)
What about
addcron:
CRONENTRY=
{ crontab -l; echo "*/2 * * * * /usr/bin/node cronapp.js >> logfile.log" } | crontab -
there you have one less pipe element...

Resources