Trouble running tar from crontab - bash

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>

Related

Why is this rsync not working?

I'm not having SSH access, only cronjob access.
I've written a copy.sh script in order to rsync the folders, but it does not happen. Why?
OLDDIR="/var/www/web46/html/magento-v2"
NEWDIR="/var/www/web46/html/magento-v4"
rsync -au $OLDDIR/media/ $NEWDIR/media >> log.txt
# rsync -au $OLDDIR/app/code/ $NEWDIR/app/code
# rsync -au $OLDDIR/app/design/ $NEWDIR/app/design
# rsync -au $OLDDIR/app/etc/modules/ $NEWDIR/app/etc/modules
# rsync -au $OLDDIR/app/locale/ $NEWDIR/app/locale
# rsync -au $OLDDIR/js/ $NEWDIR/js
# rsync -au $OLDDIR/skin/ $NEWDIR/skin
This is the target folder:
This is the destination folder AFTER the script has run:
What am I doing wrong?
Update
For any shell script running from a cron:
Use full paths. (Both in the script, and in the crontab referencing the script)
Ensure the script is made executable and that the correct user has permissions to run. (see
Regarding Magento:
Make sure you use crontab syntax applicable to magento.
See this example setup from this answer: https://magento.stackexchange.com/a/63717
*/5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=default
*/5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=always
Consider posting your question in http://magento.stackexchange.com if these basic steps don't help.
NOTE: Most tutorials / help guides / help forums, will assume, rightly or wrongly, the default position that you are a system admin, given that you are troubleshooting an admin level task. Please specify your exact constraints.
Refer to Check your existing crontab
troubleshooting page
Check your existing crontab
To verify whether or not your crontab is set up:
Log in to your Magento server. As a user with root privileges, see if
a crontab is already set up.
crontab -u <Magento file system owner name> -l
For example, on CentOS
crontab -u magento_user -l
If no crontab has been set up for the user,
the following message displays:
no crontab for magento_user
See one of the following sections for a
solution to your issue.
Solution: crontab not set up
To verify your cron jobs are set up properly, see Set up cron jobs.
This sample script adds full paths and a shebang line to your rsync script. Note: you may need to be able to test this outside of the cron to pinpoint the issue :
#!/bin/bash
echo "At least I know the script ran up to here" > /var/log/myscriptran.log
OLDDIR="/usr/share/full-path/something/magento-v2"
NEWDIR="/usr/share/full-path/something/magento-v4"
/usr/bin/rsync -au "$OLDDIR/media/" "$NEWDIR/media" 2>> /var/log/rsync_cron.log
Additionally: If for some reason you need to run in the same environment, you can always add source /User/your/home/folder/path/.profile (replace with your applicable profile path).
Crontab runs in it's own environment, you need to assume it has no knowledge of you, your home folder, relative paths, your ssh certificates location, any common environment variables / settings that you may not even be aware of, etc.
What it will always understand is full paths, and it will execute, executable files, it has permission to run.

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

Bash script failing when run by cron - mktemp outputting nothing

I have a shell script, that works when I run it manually, but silently fails when run via cron. I've trimmed it down to a very minimal example:
#!/usr/bin/env bash
echo "HERE:"
echo $(mktemp tmp.XXXXXXXXXX)
If I run that from the command line, it outputs HERE: and a new temporary filename.
But if I run it from a cron file like this, I only get HERE: followed by an empty line:
SHELL=/bin/bash
HOME=/
MAILTO=”me#example.com”
0 5 * * * /home/phil/test.sh > /home/phil/cron.log
What's the difference? I've also tried using /bin/mktemp, but no change.
The problem is that the script tries to create the temporary file in root directory when it is started from cron and it has no permission to do that.
The cron configuration file contains HOME=/. The current directory is / when the script starts. And the template passed to mktemp contains file name only so mktemp tries to create the temporary file in current directory and it is /.
$ HOME=/
$ cd
$ mktemp tmp.XXXXXXXXXX
mktemp: failed to create file via template ‘tmp.XXXXXXXXXX’: Permission denied

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

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

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