Crontab log directory? - linux

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 :)

Related

Extract data with a cron task - file with title like "name_of_file-yyyymmdd.xz"

I would like to extract some data from a file which is create each day at 3am.
The title of the file looks like this : "name_of_file-yyyymmdd.xz" ( y for year, m for month and d for day).
For my test, i try to extract this data every minutes with a cron task :
*/1 * * * * echo"`date -d 'yesterday'`">>/tmp/result.txt && xzcat /path_to_file/file-"`date -d 'yesterday' +%Y%m%d`".xz | wc -l >>/tmp/result.txt
But I have this error : "Syntax Error : premature end of file".
I don't understand because when i write "xzcat /path_to_file/file-"date -d 'yesterday' +%Y%m%d".xz | wc -l" without a cron task ( on the command window) it works.
I find the solution, if you want to make the same application, you must put a "\" in front of each "%" because it can be interpreted like a return to the line.

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

Store cron log in timedate file

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

$ * * * * * echo "hi" => blado.kdb: command not found

I'm trying to set a cron job, namely echoing "hi" every minute.
When I do * * * * * echo "hi" I get blado.kdb: command not found. Any ideas how I could fix this?
Run crontab -e in your shell. This opens a text editor
Only then type * * * * * echo "hi". Save the file the text editor just opened for you
Your Cron task is now set
PS: echo "hi" will print "hi" in a void, if you want to see some results, set a task such as * * * * * touch /tmp/foo, and you'll see the modification date being updated every minute (ls -l /tmp/foo)

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