Help with Cron job

Forum rules
Before you post please read this

Help with Cron job

Postby akya on Tue Dec 31, 2013 8:52 am

Hi, I am using cron to automate some backups.. And having trouble with one command where I find backups older than 7 days and move them to a Dump directory... My crontab reads like this -
Code: Select all
#   Minutes    Hours      Day of Month       Month    Day of Week    Command
00                      09                 *                        *                0,3              /bin/bash /home/akya/scripts/firefox-profile.sh
03                      09                 *                        *                0,3              /bin/bash /home/akya/scripts/roboform.sh
05                      09                 *                        *                0,3              /bin/bash /home/akya/scripts/mint.sh
15                      09                 *                        *                0                 /bin/bash /home/akya/scripts/important.sh
17                      09                 *                        *                0                 /bin/bash /home/akya/scripts/dropbox.sh
20                      09                 *                        *                0                 /bin/bash /home/akya/scripts/desktop.sh
15                      18                 *                        *                2,3               find /media/akya/Backup/{Desktop,Dropbox,Firefox,Important,Mint,Roboform} -type f -mtime +7 -exec mv {} /media/akya/Backup/Dump \;


To test the last command I ran
find /media/akya/Backup/{Desktop,Dropbox,Firefox,Important,Mint,Roboform} -type f -mtime -1 -exec mv {} /media/akya/Backup/Dump \; in terminal and the files I modified today were moved correctly.. But it's not working in cron. Also note that, my cron has empty last line.. so that's not the problem.. Any help please?
~Web and graphic designer... Android Developer... A Gooner~
User avatar
akya
Level 1
Level 1
 
Posts: 17
Joined: Mon Dec 23, 2013 3:15 am
Location: India

Linux Mint is funded by ads and donations.
 

Re: Help with Cron job

Postby xenopeek on Tue Dec 31, 2013 9:36 am

The path "/media/akya/Backup/{Desktop,Dropbox,Firefox,Important,Mint,Roboform}" is using Bash's brace expansion. AFAIK that won't work in cron unless you put your command either in a Bash script file or use bash -c to invoke the command.

Looking at your crontab, is it very important things are run exactly at 9 o'clock? Else if you just need them to be run once every day, you could put those kind of scripts in /etc/cron.daily.

And I never put bare commands in my crontab, always put them in a Bash script. And at the top of the Bash script put a bit like this to ensure all output of the script (like errors about something not working, and why :wink:) are logged to a file:
Code: Select all
# redirect stdout and stderr to logfile
exec >>/var/log/some-logfile.log 2>&1
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13692
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Help with Cron job

Postby akya on Tue Dec 31, 2013 9:50 am

Ahh.. that explains it.. I put it in a bash script and it worked :) Thanks.. And these scripts would work if they are kept in the cron.daily folder as it is? I just find this a bit more comfortable.. backups occur on sun, wed and cleanup occurs on monday..
~Web and graphic designer... Android Developer... A Gooner~
User avatar
akya
Level 1
Level 1
 
Posts: 17
Joined: Mon Dec 23, 2013 3:15 am
Location: India

Re: Help with Cron job

Postby xenopeek on Tue Dec 31, 2013 9:55 am

Oh, I missed that part. There also is a /etc/cron.weekly :wink: Scripts would work as-is by just moving or linking them there, as long as the scripts are executable. Nothing wrong with doing it as you have set it up.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13692
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Help with Cron job

Postby akya on Tue Dec 31, 2013 10:11 am

Thanks for the help.. I have recently switched from Win8 to Mint completely and I am loving this :D
~Web and graphic designer... Android Developer... A Gooner~
User avatar
akya
Level 1
Level 1
 
Posts: 17
Joined: Mon Dec 23, 2013 3:15 am
Location: India


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests