Can I create a crontab "on the fly" in a script? - bash

I have a script that creates some temporary files that need to remain in place for quite some time. I want the user who executes the script to be able to create their own custom crontab that removes these files at a later time.
To test, I've just simply tried to setup a simple crontab using the command-line exclusively, but I'm not sure if this is even possible.
From the command-line I type the following:
$ crontab 1 * * * * $(mkdir -p ~/Desktop/CronSuccess)
I get the error: crontab: 1: No such file or directory
Is there anyway to have a script create a fully functional crontab on the fly?

Read the manpage for crontab
You can't do what you're trying to do.
You can however have crontab read the entries from a file.
e.g.
echo 'crontab 1 * * * * mkdir -p ~/Desktop/CronSuccess' >mycrontab
crontab mycrontab
Be aware that this will not append to the users crontab, it will replace the existing crontab of the user
with what's in the mycrontab file.
Surrounding the mkdir command with $() would be wrong.
You might also use at if it suits your needs:
e.g.
echo 'mkdir -p ~/Desktop/CronSuccess' | at now + 10 hours

Related

How to configure crontab to run ec2-automate-backup.sh script

Hi guys am trying to automate the backup of snapshots for my ec2 volumes on Amazon. I am following the ec2-automate-backup script by Collin Johnson
If run the command on command line it is creating the snapshot (working):
ubuntu#linuxserver:/usr/local/ec2/scripts$ sudo ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
For testing purposes if i create a crontab its not working
0 10 * * * ubuntu /usr/local/ec2/scripts/ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c /usr/local/ec2/scripts/cron-primer.sh -r "eu-west-1"
Where is my problem here am running the script on ubuntu 14.04 - Amazon?
In crontab file, to execute a shell script you can use one of the following approach:
1. Call the shell script direcly, i.e.
0 10 * * * /path/to/script.sh
where the script.sh should be made executable.
2. Execute the script by sh utility, i.e.
0 10 * * * sh /path/to/script.sh
here the script.sh need not be made executable.
Now, if in your case, you need to go to a specific path and then execute script, then :
Either provide the full path of the script in crontab file directly, or
Enclose the execution commands in other shell file, and execute the enclosing file from cron.
There are two possibilites:
You need root access to run the script. You can solve this by modifying root's crontab:
sudo crontab -e
See How to run a cron job using the sudo command
You need to be in the same directory as the script to execute it
0 10 * * * ubuntu cd /usr/local/ec2/scripts && ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
See What is the 'working directory' when cron executes a job

Running a Shell Script as a Cronjob

I have a written a shell script to automate a build process.
The script checksout some code from an SVN repo, compiles and builds the code before extracting the built binary files and storing these in a central location.
I can manually execute the script ./autobuild.sh and it runs perfectly. There are a few sudo commands executed throughout the script, but I echo the password through for the first sudo command and the password holds for the entire time:
echo mypassword! | sudo -S make clean
When I add executing the script as a crontab it fails to complete all the tasks. I've tried to add it as a cronjob for the normal and root users.
Running crontab -e on my normal user account, I want the script to run at ten past midnight every day:
10 0 * * * /home/username/autobuild.sh
Also running a 32-but Cent OS 7 install with all the latest updates installed.
Can anyone provide any suggestions as to why it might work manually but not when run through a cron?
Try this
10 0 * * * /bin/bash /home/username/autobuild.sh
Step 1. find bash path
:~# whereis bash
Output
bash: /usr/bin/bash
step 2. create sh file add the line on top replace with your bash path
#!/usr/bin/bash
step 3. Make the script executable with command chmod +x .
step 4. add cron like this in for every minute to test
crontab -e
*/1 * * * * /usr/bin/bash ~/backup.sh >>test.log

Creating crontab for non-root user

I am attempting to run a script through crontab that is required to run as an oracle user. I have tried creating a crontab for that user by:
su -u oracle crontab -e which has allowed me to create one. I edited the file to run a perl script:
0 5 * * * /usr/bin/perl /path/master.pl > /tmp/debug.log
However when the time passes nothing is run.
Is this the proper way to create a crontab for non-root user? Also the master.pl file call multiple scripts that also need to be done as a oracle user if that makes a difference.
Use crontab's -u option. The man page says:
-u Appends the name of the user whose crontab is to be modified.
If this option is not used, crontab examines "your" crontab,
i.e., the crontab of the person executing the command. Note
that su(8) may confuse crontab, thus, when executing commands
under su(8) you should always use the -u option. If no
crontab exists for a particular user, it is created for him
the first time the crontab -u command is used under his
username.
So the correct command is:
sudo crontab -e -u oracle

Trouble running tar from crontab

When I want to compress a folder manually I use the command:
tar -zcvf $dirBackup-$actualTime.tgz $dirBackup;
And it works perfectly. Also when I decompress the archive the result is correct.
But I have a problem when I run the same command from crontab: the resulting tgz only appears to contain an empty file.
crontab -l>> /home/user/Desktop/test/temp;
echo "$min $hour * * * tar -zcvf $dirBackup-$actualTime.tgz $dirBackup">> temp;
crontab temp;
Do I need to change anything in the crontab expression? Do I have to be root to run crontab? The file temp is created correctly, and located in the right folder.
Thanks a lot for your attention!
Depending on your system, crontab jobs may or may not be run in your $HOME directory (in fact they are typically run either in / or in /var/cron). Add a cd $HOME; before the tar command.
You need to set the value of the variables in the echo command you are using to add entry into the cron job. A sample script should look like this:
#!/bin/bash
min=30
hour=17
dirBackup=/home/user/Desktop
actualTime=30-04-2015
crontab -l >> /home/user/temp
echo "$min $hour * * * tar -zcvf $dirBackup-$actualTime.tgz $dirBackup 2>&1 | tee /tmp/cron.log">> /home/user/temp;
crontab /home/user/temp;
The resulting tar file will be created in the user's HOME directory (user with which the cron is run).
To avoid getting into user permission issues, you can specify the user with which the cron is to be run using the following command:
crontab -u <username> <cron_file>

Cron job does not start [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed last month.
I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?
Your $PATH is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch, chmod, curl) are in /bin and/or /usr/bin.
How did you set up the cron job? Did you run crontab some-file as root?
It seems that /etc/crontab is the usual mechanism for running cron commands as root. On my Ubuntu system, sudo crontab -l says no crontab for root. Running crontab as root, as you would for any non-root account, should be ok, but you might consider using /etc/crontab instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of /etc/crontab:
$ head -5 /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.
Run sudo crontab -l. Does it show your command?
Temporarily modify your script so it always produces some visible output. For example, add the following right after the #!/bin/sh:
echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
and see what's in /tmp/scheduleSpider.sh.log after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add more echo commands to your script to see in detail what it's doing.
It looks like your script is designed to run only once; it creates the sync.txt file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to delete sync.txt after running the command, and just forgot to do it?
root's home directory on Ubuntu is /root. The first time your script runs, it should create /root/sync.txt. Does that file exist? If so, how old is it?
Note that curl someLink (assuming someLink is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail to root? Or did you just not show us the entire command?
First: you can substitute the first field with */5 (see man 5 crontab)
Second: have cron mail the output to your email address by entering MAILTO=your#email.address in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).
A better syntax for you CRON is
*/5 * * * * /scr_temp/scheduleSpider.sh
Also, check the authority of your scheduleSpider.sh file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Try chmod 777 for now, just to check.
I suggest to:
check that /scr_temp/scheduleSpider.sh has executable bit
set PATH properly inside your script or use absolute path to command (/bin/touch instead of touch)
specify absolute path to sync.txt file (or calculate it relatively to script)
Have you added the comand via crontab -e or just by editing the crontab file? You should use crontab -e to get it correctly updated.
Set the working directory in the cron script, it probably doesn't execute the things where you think it should.
You should add /bin/sh before the absolute path of your script.
*/5 * * * * /bin/sh /scr_temp/scheduleSpider.sh

Resources