Cron jobs on Ubuntu EC2 Instance

0

Help please, I can't seem to get cron jobs to run. I've always done crontab -e to schedule jobs but it doesn't seem to work on my EC2 instance running Ubuntu. I also tried sudo vi /etc/crontab and that doens't work either. I tried to schedule the job /usr/bin/echo "test" > /home/ubuntu/crontest.txt in /etc/crontab as root and as ubuntu but I never see the test file written. I googled and there are a few StackOverflow posts from people who had trouble with cron. But it seems many of them are able to get it to work after editing the /etc/crontab file. Can someone help me figure out why I can't get my jobs to run in cron?

Any help or pointers would be greatly appreciated. Thank you.

gefragt vor 2 Jahren4560 Aufrufe
2 Antworten
0

Thank you Sri for your detailed response! For your local crontab -e example, I don't think you want to specify the user. After seeing your post, I tried crontab -e again and installed a * * * * * test script to echo the date into a test file and it worked. I was able to see the test file and the date written inside was updated every minute.

Unfortunately, when I change it specify the hour and minute, the scheduled job does not run. I suspect this is due to time zone issue so I added a CRON_TZ=America/Los_Angeles in my crontab for PST time zone. That didn't work either. Then I tried scheduling using UTC time and that worked....

So, how do I change the time zone for crontab? My system time is already set to PST. I see it when I type date in the shell. The man page for cron says "The daemon will use, if present, the definition from /etc/timezone for the timezone." when I cat /etc/timezone it says "America/Los_Angeles".

I'm not sure why there's a time zone mismatch.

beantwortet vor 2 Jahren
0

Hi, Good Question

I was curious about this, so I have setup a Ubuntu 20.04 and I was able to use crontab.

crontab's are generally setup per user. when you login, you would be logging in as a ec2-user. Let's assume that you have logged in as ec2-user, so you could check if there is a crontab by running crontab -l. If there is no crontab then you would see something like the following

crontab -l
no crontab for ubuntu

to add a crontab, you could use crontab -e, use the editor of your choice and add a line like

17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly

then restart sudo service cron restart Next you can check sudo service cron status and then crontab -l returns

17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly

If you need to setup crontab for root, you can use sudo crontab -e or if it's for another user then crontab -u

Also check if you have this file

cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

profile picture
Sri
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen