add bash script to crontab via bash script - python

I would like to add date with filename to my bash script which would be added to crontab file via script. The problem is entry in crontab file is already having the date appended. But my requirement is to have the date command in crontab.
crontab -l > "$FILENAME"
if grep -i cron "$FILENAME"; then
echo "Cron Job already present in User's crontab file"
else
echo "*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +\%Y\%m\%dT\%H\%M\%S`.log 2>&1 " >> mycron
crontab mycron
echo "Crontab added to User's Crontab"
fi
Actual :
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_20211217053830.log 2>&1
Requirement :
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +%Y%m%d%H%M%S`.log 2>&1
I have also tried to add escape characters but didn't seem to work

Use cat
cat << '==CRONTAB_LINE==' >> mycron
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_$(date +%Y%m%dT%H%M%S).log 2>&1
==CRONTAB_LINE==
Result
crontab -e
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_$(date +%Y%m%dT%H%M%S).log 2>&1

It looks like your crontab entry is close. Since %'s are special (newline) characters in a crontab file, escaping them should give the desired result:
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +\%Y\%m\%d\%H\%M\%S`.log 2>&1
The echo-line that accomplishes this is:
echo "*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_\`date +\\%Y\\%m\\%dT\\%H\\%M\\%S\`.log 2>&1 " >> mycron
I verified that this works in Ubuntu.
Additionally, adding the line SHELL=/bin/bash to the crontab file (above the entries) ensures that bash interprets the crontab entries. While you likely will not need it it this case, it would enable more possibilities.

To insert the command output you have to replace
`date +\%Y\%m\%dT\%H\%M\%S`
with $(date +\%Y\%m\%dT\%H\%M\%S)

Related

Execute a Python script with Crontab

I'm trying to execute a python script using crontab. I want to run this script every 2 hours. I use:
export EDITOR=nano
crontab -e
then:
0 */2 * * * cd /Users/myname/Desktop/CoronaFR/ && /opt/anaconda3/bin/python3 CoronaFR.py
But it doesn’t work. Any idea? Maybe a path problem?
Thanks in advance!
You can remove cd command and directly call the script by this command:
0 */2 * * * /opt/anaconda3/bin/python3 /Users/myname/Desktop/CoronaFR/CoronaFR.py
Try
0 */2 * * * /opt/anaconda3/bin/python3 /full/path/to_the_script/CoronaFR.py

Cron job wont run python script - even after two days of googling

I am trying to run python script via a cron job, but have had no luck the last two days, and I am running out of hairs to pull out.
Some info:
-->I have approximately 20 hours experience using Linux, so I might have missed something very basic.
-->Linux server (Ubuntu) on Linode.com
-->Script runs in terminal
-->Script has the permissions "0644"
-->#!/usr/bin/env python3.7
is added to the beginning of the script
-->The script belongs to the user "adamsavage" and I have tried to add it to both this users cron file and the cron file belonging to root using crontab -e and sudo crontab -e respectively
-->The cron files looks like this, and has a newline at the end:
* * * * * /usr/bin/python3 /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
-->sudo grep CRON /var/log/syslog returns this:
Apr 2 15:25:01 noeluddig CRON[7728]: (adamsavage) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7729]: (root) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7730]: (adamsavage) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7731]: (root) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7732]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
-->I have also tried the following:
* * * * * cd /home/adamsavage/python_scripts/ && /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
and
* * * * * cd /home/adamsavage/python_scripts/ && /usr/bin/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
-The script is supposed to send me an sms with some info, which again works when it is ran from terminal.
-I should also mention that just having echo "message" >> /home/adamsavage/ouput.txt does actually run and prints "message" to that file.
What am I missing? Suffice to say, help will be greatly appreciated!:)
You should run your python script with python command:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
For example:
* * * * * /usr/bin/python3 /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
Try to add 2>&1 at the end of your cron line:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
I have no idea why this is, but what made it work was to remove the absolute path to the log file. If anyone can explain this, that would be great!

Cron is not able to execute python script

working on windows 10 using putty. I want to schedule a task using cron but its not working for the python code(sowfinal.py) which I want to run but I am to execute a simple python code(sowtest.py)... is it because of the libraries I am using ?
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowtest.py /tmp/listener.log 2>&1
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowfinal.py /tmp/listener.log 2>&1
Try without cd
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowtest.py /tmp/listener.log 2>&1
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowfinal.py /tmp/listener.log 2>&1

cron couldn't run Scrapy

The code in crontab 0 * * * * cd /home/scrapy/foo/ && scrapy crawl foo >> /var/log/foo.log
It failed to run the crawl, as there was no log in my log file.
I tested using 0 * * * * cd /home/scrapy/foo/ && pwd >> /var/log/foo.log, it echoed '/home/scrapy/foo' in log.
I also tried PATH=/usr/local/bin and PATH=/usr/bin, but no success.
I'm able to run it manually by typing cd /home/scrapy/foo/ && scrapy crawl foo in command line.
Any thoughts? Thanks.
Problem solved. Rather than running the crawl as root, use crontab -u user -e to create a crontab for user, and run as user.

Variables in Crontab Amazon Linux

Can't get crontabs with variables working on Amazon Linux. It's weird cause those variables do work in the command line of the system, but won't work inside the cronjob. I know the variables are the problem, cause when I remove the part from --argstill >, the cronjob will be executed.
Worked fine on Ubuntu:
0 7 * * * /usr/bin/python /path/to/cronjob.py --method all --args $(date -d "1 day ago" +%Y%m%d),$(date +%Y%m%d) > /dev/null 2>&1
Also tried:
47 16 * * * python /path/to/cronjob.py --method all --args `date -d "1 day ago" +%Y%m%d`,`date +%Y%m%d` > /dev/null 2>&1
#tedder42 solution worked for me, I only needed to add the backslashes before the %:
47 16 * * * python /path/to/cronjob.py --method all --args `date -d "1 day ago" +\%Y\%m\%d`,`date +\%Y\%m\%d` > /dev/null 2>&1

Categories

Resources