Cameron Kilgore
Cameron Kilgore

Reputation: 423

Create crontab for specific user

What is the most optimal approach for creating a crontab for a specific user, as part of a Packer build process? Much of what i've seen covers using the crontab utility as a text editor, and does not specify using pipes or output to load the file. This operation would be run from sudo, since the cron directories are root-owned directories.

Upvotes: 0

Views: 1807

Answers (2)

VimalKumar
VimalKumar

Reputation: 1850

Assuming that you have your script copied to /home/ec2-user/scripts/test.sh, below one liner will setup the crontab to run the script. We use this in our Packer shell provisioner to setup the crontab entries. Below sample entry runs every minute. You can change the timing.

(crontab -l 2>/dev/null; echo "* * * * * /home/ec2-user/scripts/test.sh") | crontab -

Upvotes: 0

Romeo Ninov
Romeo Ninov

Reputation: 7225

The simplest way to "import" file in to cron record is to use command like this:

crontab input_file

the input file should contain records on the same way they are stored in cron files, something like

1 2 3 4 5 /path/to/executable

Upvotes: 1

Related Questions