Create crontab for specific user - cron

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.

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

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 -

Related

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

Simple cron with flock not working on Ubuntu

I've created the file crontester on /etc/cron.d with this line on it:
* * * * * /usr/bin/flock -n /tmp/fcj.lockfile touch /tmp/test.txt
That should be running every minute.
But I dont see the /tmp/test.txt file being created, so the cron is not working correctly.
What I'm doing wrong? Do I've to create the /tmp/fcj.lockfile, if I've to do this, do I have to create it empty?
Thanks a lot.
The command runs fine on my machine, so the cronjob is probably not set up properly. man cron discourages creating /etc/cron.d/ files:
Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.
Try creating the cronjob using crontab -e and see if it works

Scheduling jobs in Linux using crontab

I am using Linux Centos to schedule a job.
I have created a shell script file called Im_daily_loads.sh to run the job at 12:42PM everyday.
with the following comands:
#!/bin/sh
42 12 * * * cd $pdi; ./kitchen.sh -file="/opt/kff/software/pdi/5.0.1.A/data- integration/projects/IML/code/stg/IML_Load_Frm_SRC_To_PSA.kjb" -level=Basic > -logfile="/opt/kff/software/pdi/5.0.1.A/data-integration/projects/IML/log/iml_daily_loads.err.log"
Then loaded the file into crontab by using the issuing the following command crontab Im_daily_loads.sh, but my job is not running.
What would be the problem?
Why not just use
crontab -e
as the user you plan to execute the job as, enter the job, save and exit the editor?
Also, it looks like you need to define $pdi in your script. How is crontab supposed to know where your script is located?
first , run a very simple job to be shure crontab works at all.
for example
set > /tmp/crontab_works.log 2>&1
it will write down all variables. so you will see not all variables available in crontab

Script runs from terminal, but not cron. What edits to this script do I need to make?

I have a script used for zipping a database and site files, then dumps the output into a backup folder on the server. The script runs fine from the command line, but it will not work through cron.
After much research, I am thinking that cron cannot run it in its current form because it runs in a different environment.
Here is the script, saved as file_name.sh
#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="website.com.$NOW.tar"
BACKUP_DIR="/backupfolder"
WWW_DIR="/var/www/website/"
DB_USER="dbuser"
DB_PASS="dbpw"
DB_NAME="dbname"
DB_FILE="website.com.$NOW.sql"
WWW_TRANSFORM='s,^var/www/website,www,'
DB_TRANSFORM='s,^backupfolder,database,'
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
I currently have the script stored in /usr/local/scripts/
Is there something wrong with the above code that does not allow it to run through cron?
Which crontab should it go in? crontab -e from terminal, or /etc/crontab? They are two different files.
Several things come to mind: first, one of the most common problems with cron jobs is that generally crond runs things with a very minimal PATH (usually just /usr/bin:/bin), so if the script uses any commands from some other binaries directory, it'll fail. Where is mysqldump on your system (run which mysqldump if you aren't sure)? If this is the problem, adding PATH=/usr/local/bin:/usr/bin:/bin (or whatever's appropriate in your case) at the beginning of your script should fix it. Alternately, you can set PATH in the crontab file (put this line before the entry that runs your script).
If that's not the problem, my next step would be to capture the script's output, with something like:
1 1 * * * /usr/local/scripts/file_name.sh >/tmp/file_name.log 2>&1
... and see if the output is informative. BTW, as #tripleee mentioned, the format of your cron entry is suitable for the files crontab -e edits, but not for /etc/crontab. The /etc version has an additional field specifying which user to run the job as, e.g.
1 1 * * * eric /usr/local/scripts/file_name.sh >/tmp/file_name.log 2>&1
Best practice is to always use crontab -e (the resultant files are usually in /var/spool/cron/) and this works on every unix and linux platform I ever worked on.
Other common issues with cron execution are missing environment variables. Any environment variables set in .bash_profile (or .profile if you use korn shell) will not necessarily be present in the cron environment. This can be overcome by including them in your script.
As Gordon said, paths are another suspect. You can always full path you executables in your script (eg /bin/mysqldump). Some of the more cynical of us do this anyway to make sure we are executing what we intended as apposed to some other file of the same name in the current path.
I can only guess at your specific problem since you fixed it by creating /scripts, that perhaps the permissions on /usr/local/scripts directory did not allow execution by the cron user?
I have had to remove the extension (.sh) for cron to run in some instances.
So I fixed it. Not sure what the problem was, but this worked for me.
I originally had the scripts located in /usr/local/scripts/
I created a new directory here - /scripts/ and moved the scripts there. The new crontab -e command looked like this:
1 1 * * * bash /scripts/file_name.sh
Works perfectly. Again, I am not sure what the issue was before, but it works now.

Confusion about Crons

this is my first time writing script for cron job.
I wrote my code in shell, (which it works) and I'm trying to set it up for cron.
So here is my question. How do I set up the cron? Am I suppose to write
10 * * * * /home/workstation/deleter.sh (I want it to run every 10min)
right underneath #!/bin/sh? How would I execute it? (deleter.sh has permission via chmod)
man 1 crontab returns "No entry for crontab in section 1 of the manual"
I'm really lost and confused right now. If someone know how to set up cron please tell me!!
Thanks in advance
#!/bin/sh
counter=0
logloc=/home/ServerLogs
backup=/home/test
## Reads the location of the file systems that needs to be investigated from location.txt
## and save it into an array
while read -r line; do
Unix_Array[${counter}]=$line;
let counter=counter+1;
done < location.txt
## Reads Email recipients and save it into an array
More code continues from here......
The following will open your environment's text editor and load the crontab:
crontab -e
Your crontab entry is mostly correct. In order for your script to run every ten minutes it should be changed to:
*/10 * * * * /home/workstation/deleter.sh
The entry you indicated would run the script at the 10th minute of every hour.
To setup the cron, you can do one of two (main) things. The first would be to place the specified line in /etc/crontab. The second would be to run crontab -e and place the line in there. I would recommend to use crontab -e so the cron will execute as your own user account.
If the full path to the script is /home/workstation/deleter.sh and it does have execute-privileges, as you specified - your current line will have it execute 10-minutes past the hour, every hour. To get it to execute every 10 minutes, you'll have to use */10, like this:
*/10 * * * * /home/workstation/deleter.sh
this might help
http://www.manpagez.com/man/5/crontab/
you need to get an entry into your crontab
One of the best links I came across when I first learned about cron! Bookmark it
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

Resources