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
Related
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)
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!
I followed the suggested solution for this question.
And came up with the following docker file
FROM ubuntu:16.04
ADD write_time.py /
USER root
RUN apt-get update && \
apt-get install -y python cron && \
chmod +x /write_time.py && \
(crontab -l 2>/dev/null; echo "* * * * * cd / && /usr/bin/python /write_time.py >> test.out") | crontab -
the write_time.py is
#!/usr/bin/env python
import datetime
time = datetime.datetime.now()
time = time.strftime("%Y-%m-%dT%H:%M:%S.%f")
print(time)
with open("time.txt", "a") as f:
f.write(time+"\n")
After i build with the below command and run it -
docker build . -t se
docker run -it se
I exec into the contaier, to check if either test.out or test.txt is created at / but i do not see either.(have waited for more than 2 mins)
Anything i am doing wrong here?
Thanks!
Solved it.
Docker CMD needs to be set to cron deamon.
on Ubuntu 16 my crontab has the following:
0 8 20-31 2-12/2 * [ $(date +\%d -d "7 days") == 01 ] && python3 myscript.py &
I expected this to kick off this morning but it didn't. It kicked off just fine if changed to:
0 8 25 2-12/2 * python3 myscript.py &
Any suggestions on how I can crontab to kick off a script 7 days before the end of the month?
Cronjobs are run by /bin/sh by default. Your syntax is not valid for sh. Try setting SHELL=/bin/bash at the top of your crontab to run all jobs using bash instead.
I am trying to run a few python scripts through Crontab in Centos docker container, but nothing I have tried is working.
Firstly I installed cron:
yum install vixie-cron
Then I ran it as a service:
/etc/init.d/crond start
(I also ran /sbin/service crond start because some answers to related questions suggested so)
ps aux | grep cron shows:
root 16917 0.0 0.0 23288 1252 ? Ss 18:53 0:00 crond
root 16929 0.0 0.0 9720 836 pts/0 S+ 18:55 0:00 grep cron
crontab -l looks like:
0 17 1 * * /root/proj/env/bin/python /root/proj/files/frontend/file1.py > /var/log/cron.log
0 9 4 * * /root/proj/env/bin/python /root/proj/files/frontend/file2.py > /var/log/cron.log
0 17 15 * * /root/proj/env/bin/python /root/proj/files/frontend/file3.py > /var/log/cron.log
0 9 18 * * /root/proj/env/bin/python /root/proj/files/frontend/file4.py > /var/log/cron.log
0 14 * * * /root/proj/env/bin/python /root/proj/files/frontend/file5.py > /var/log/cron.log
0 8 * * * /root/proj/env/bin/python /root/proj/files/frontend/file6.py > /var/log/cron.log
* * * * * echo 'Check!!' > /var/log/cron.log
All python scripts and the cron.log file have permission 777
The last entry in crontab is just to check if anything is getting written to the log file... but nothing is being written there..
Any idea how to solve this?
PS: I looked through a ton of related QnAs but none of them helped.
Some answers had suggested writing to /etc/crontab.. so I even made the entry: * * * * * root echo 'Blah' > var/log/cron.log there.. but to no effect :(
After a day wasted in suboptimal Googling and experimentation, the answer to this conundrum was commenting out the following line in the file /etc/pam.d/crond (and then running service crond restart):
session required pam_loginuid.so
This is because of some Docker-Centos security issue. I don't have further details on why this works. (Check this for details.)
Also, troubleshooting this made me realize a few gotchas which might be useful:
Check that you have crond running as a service (if not, use
/etc/init.d/crond start)
Each line in your crontab should be followed be a newline character
Your scripts and log file should have suitable write/execute
permissions
crontab file shouldn't have weird Windows environment characters
(like ^M)
In some rare cases, crond might have different time zone than your
system
Also check /etc/cron.allow and /etc/cron.deny files to verify who
can add/edit cron jobs
May your day be invested in more fruitful pursuits, stranger.
I recommend run cron under host and run command inside the container
example:
0 17 1 * * /usr/bin/docker exec container_name /root/proj/env/bin/python /root/proj/files/frontend/file1.py > /var/log/cron.log