Installation and managing of crontab using AWS linux AMIs

Gabriel Hidalgo Ruiz
2 min readFeb 4, 2021

Crontab installation on Linux AMI

This will in addition install several dependencies, including cron.

$ yum -y install crontabs

$ chkconfig crond on

$ sudo service crond start

$ sudo service crond status

Edit the crontabs

Remember to do this with su or root, otherwise, there might be access issues with the actual items to run. vi is the default editor, but I like nano better, so:

$ export VISUAL=nano; crontab -e

Add a crontab entry like this into the file:

* * * * * <root_to_script>
contab times explanation
For more information about this configuration, you can visit https://crontab.guru/, where you could see all your possibilities.

for example:

*/30 * * * * /home/ec2-user/thumbnail-stone/scripts/my-script.sh

This line runs the crontab every 30 sec. executing the script “my-script”. my-script must have to look something like this (if you use an nvm node installation):

#!/bin/sh
~/.nvm/versions/node/v10.23.2/bin/node thumbnail-stone/scripts/cron-task.js

or if you DON’T have installed node using an nvm distribution:

#!/bin/sh
node thumbnail-stone/scripts/cron-task.js

If a crontab is created as root user maybe the routes are not the same that here are specifying. if your task not works you can be sure that is not a path error looking the crontab logs shown below.

Crontab logs

If you are working with a Linux AMI of AWS, the route of crontab logs are:

/var/spool/mail/<user>

Where user is the user who created the cron task. For example, if the task creation was created writing sudo crontab -e, user must be equal "root" and the path should be /var/spool/mail/root. On the other hand, if a task was created as crontab -e, user must be equal to "ec2-user" or another user name which you are working.

Managing crontabs

The crontab command allows you to view, edit or replace a user’s crontab file. This list shows the common options for the crontab command. Notice that only the root user can change other user’s crontabs.

  • crontab -e: Edit the current crontab file using the text editor specified by the EDITOR environment variable or the VISUAL environment variable
  • crontab -l: List the current crontab file
  • crontab -r: Remove the current crontab file
  • crontab -u: Specifies the user’s crontab to be manipulated. This is usually used by root to manipulate the crontab of other users or can be used by you to correctly identify the crontab to be manipulated if you have used the su command to assume another identity.

--

--