2
votes

I am struggling to get crontabs in my AWS EC2 instance (Amazon Linux AMI) to run an R script of mine, so I wrote a very basic cronjob simply to test if crontabs are working in my instance at all:

crontab -e

* * * * * echo "Is this working" 
* * * * * echo "write to script" >> testfile.txt

save the crontab

however neither of these did anything (no output in terminal from the job running / no new textfile written in my home directory)...

how to I get crontabs to work in my AWS EC2 instance?

EDIT: I tried to run the following in the command line, which didnt work...

sudo service cron start
cron: unrecognized service

EDIT2: I also get the following

ps aux | grep crond
root      2694  0.0  0.0 121604  2580 ?        Ss   Jan10   0:00 crond
root      5491  0.0  0.0 110472  2044 pts/0    S+   00:52   0:00 grep --color=auto crond
1

1 Answers

3
votes

You can set environment variables for cron.

Do you try to check following environment variables ?

  • HOME
    • It indicates cron user's home directory. I guess it's defined as "/"
  • PATH
    • It defines cron user's $PATH. Please this variable also.

You can see example crontab from /etc/crontab

$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# 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