Results 1 to 8 of 8
I'm fairly new to linux. Know my way around the shell and am not afraid to get dirty. The next little task I want to do is create a cron ...
- 11-18-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
Linux newbie and scripting
I'm fairly new to linux. Know my way around the shell and am not afraid to get dirty. The next little task I want to do is create a cron job, or some sort of script to run lets say, once a day. I want it to basically copy all files from /home/myaccount/mystuff/* to /home/myaccount/mynfs_backup/ that have a modified date which is 'after' the ones currently in the destination directory. I'm sure this is a fairly simple task, and I don't want you to tell me the answer straight up, but pointing me to a guide or manual to get this started would be greatly appreciated.
**EDIT** What I already know. I'll be using 'cp -u -r <source> <destination>'. But would I place this into lets say "backup.sh" and then make a cron job to execute this at 24 hour intervals?
Thanks,
Marf
- 11-18-2007 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
If it's a single command, you can place that command directly in the crontab. Although there's nothing wrong with writing a script to execute the command if you want. If you write a script it's a good idea to enable verify mode in case of problems:
#!/bin/bash -vx
Look at manpages 'man crontab' & 'man 5 crontab' for more information about crontab. I suggest you pipe all output from the cron job to a log file in case of errors like this:
command > file.log 2>&1
Also, when cron jobs run they don't execute your profile so things like PATH may not be defined as when logged in interactively. Therefore, when you execute commands it's best to specify the full path like /bin/cp instead of just cp.
Hope this helps.
Vic
- 11-18-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
thanks for your response I'll give it a shot
Also
will this create a file.log file if it doesn't exist, and what does 2>&1 do?command > file.log 2>&1
- 11-19-2007 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
Yes file.log will be created if it doesn't exist. Also, if it already exists a new one is created.
The 2>&1 redirects stderr (standard error) channel (2) to stdout (standard output) channel (1). Normal output goes to stdout and error messages usually go to stderr. This will cause all output to go to file.log. The line can also be like this:
command 1> file.log 2>&1
Try this:
ps -f6 > file.log
You should see an error message on the screen.
ps -f6 > file.log 2>&1
The error message will be in file.log.
Look at the REDIRECTION section in 'man bash' for details.
Vic
- 11-24-2007 #5Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
ok I have been playing around and all is working well. Just have a question. Not only do I want errors to be outputted to the file, I also want the successful copies to output to the file but it doesn't appear to be happening. I input the "cp -u -v -R ...."
in hopes that the verbose flag outputs the steps to the file but that didn't happen.
Thanks again for your help.
- 11-24-2007 #6
What's the log file like at this point? Does it even exist after running the job? Is it empty?
Could you post the cron job script?
Is the log file specified with a full path name (beginning with /)?--
Bill
Old age and treachery will overcome youth and skill.
- 11-24-2007 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
You might also consider using my favourite cp flag: -p to preserve file timestamps.
- 11-24-2007 #8Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
haha I got it to work. Such a stupid reason I won't say

Thanks again for your quick replies!


Reply With Quote