LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-18-2008, 12:29 PM   #1
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Rep: Reputation: 0
Using crontab to move files


I am new to the linux environment, so bear with me. I am trying to setup crontab to move files from one directory to another automatically. These are files that are ftp to this server in a specific input directory. Once the file is run it is renamed and left there. What I want to do is have it move the file to another backup directory of my choosing. I have created a file call CSF Move File and the command I placed it in is:

mv /opt/novell/dtf/input/*.bak /opt/novell/dtf/input/backup

I placed this file in the cron.hourly directory under the etc main directory. I made it also executable. I have waited two hours now and nothing had happened. Is there something else I need to do to make this happen? Any help would be appreciated.
 
Old 04-18-2008, 01:00 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790Reputation: 790
Put in the full path for the "mv" command. That is to say instead of just "mv" put in "/bin/mv" so it knows where to find the "mv" command.

Cron isn't using the same environment you use when you login. Commands you do from the command line often don't work in Cron due to this. The most common part of the environment you have that cron doesn't is what is in your PATH variable.

You can see what is in your PATH variable by typing "echo $PATH" and hitting return.

Also to verify where the "mv" command is type "which mv".
 
Old 04-18-2008, 01:24 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Is the cron daemon running? What is the content of /etc/crontab? I've just tried on my Opensuse 10.3 and it works like a charm. What happen if you try to execute the script from the root crontab (that one created by crontab -e)?
 
Old 04-18-2008, 01:32 PM   #4
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
OK, I added the /bin to the full path of the mv part of the command line. Is there a way to make this run automatically to see if it worked? I place the file in the cron hourly directory. The daemon wasn't running, but I figured out how to start it and it is running now. I tested the script and it worked great. Now that the file is in the cron hourly directory, what else do I need to do to make sure it runs each hour? Is there anything I need to add to the crontab file in the /etc directory?

Last edited by scstech; 04-18-2008 at 02:06 PM.
 
Old 04-18-2008, 05:35 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by scstech View Post
Now that the file is in the cron hourly directory, what else do I need to do to make sure it runs each hour?
To be sure it runs each hour, you can add a date command and append output to a file. Also you can check the exit status of the mv command to see if it was successful, for example (immediately after the mv command) you can put
Code:
(echo -n "$?  " && date) >> /root/log.hourly
after some time check if the log.hourly file contains the correct dates, like this
Code:
0  Fri Apr 18 19:00:01 CEST 2008
0  Fri Apr 18 20:00:00 CEST 2008
0  Fri Apr 18 21:00:00 CEST 2008
0  Fri Apr 18 22:00:01 CEST 2008
when you have verified that all works properly, you can remove that line from your script.
 
Old 04-18-2008, 08:42 PM   #6
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I will append the command line to the end of the mv command I already have in that file. Up until now it hasn't moved a file yet. I will let this run to see what happens with this command appended to the end of the current mv command. I will let you know what happens.

I assume when you say immediately after the mv command, put a space then the command you gave, correct? This is what I have now in the file:

/bin/mv /opt/novell/dtf/input/*.bak /opt/novell/dtf/input/backup (echo -n "$? " &&date) >> /root/log.hourly

Last edited by scstech; 04-18-2008 at 08:43 PM.
 
Old 04-19-2008, 03:29 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
No no... you don't separate commands with a single blank space! Press ENTER and start a new line or eventually put a separator specific to the language (in bash it is semi-colon). The line above may have unpredictable results. Anyway, you asserted that the script worked great. What is wrong with the move command now?
 
Old 04-20-2008, 01:09 PM   #8
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I mean when I run it manually. It doesn't do it automatically though. That is what I can't seem to figure our why it won't. I created a file and place that command in it. I then place the file on the cron.hourly directory. The daemon is now running, so I am not sure what else I need to do to make it work.

So you mean place the second command you gave me like this in the file?

/bin/mv /opt/novell/dtf/input/*.bak /opt/novell/dtf/input/backup
(echo -n "$? " &&date) >> /root/log.hourly

Last edited by scstech; 04-20-2008 at 01:11 PM.
 
Old 04-20-2008, 03:19 PM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes. You mean the file /root/log.hourly is not being created?

Can you try to add the command to a "user" crontab? For example, as root you can do
Code:
crontab -e
this will open an empty file in the vi editor. Assuming you have an executable copy of your script in /root/script.sh, you can simply add the following line:
Code:
0 * * * * /root/script.sh
save and exit. The crontab will be written in /var/spool/cron/tabs/root and will be added to the scheduled tasks at most one minute later. Please, try this and tell us if it works. For detail about the crontab format (if you are not experienced with that) look at
Code:
man 5 crontab
 
Old 04-21-2008, 08:26 AM   #10
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
That file has not been created thus far. That is if the last example I gave with the format you told me is correct. What do you mean by this statement:

Assuming you have an executable copy of your script in /root/script.sh, you can simply add the following line:

Sorry, I am new to this and I didn't understand what you meant by this statement. Also, didn't understand the part about having an executable copy in the /root/script.sh.

Does the file I create with the move command and all have to have a certain type of extension on it? My understand of it was that I just create a file with the command in it and just place it in the cron.hourly directory and as long as the cron daemon is running it would work? Is that not right?

I apologize again for not having a better understand of what your asking.

Last edited by scstech; 04-21-2008 at 08:36 AM.
 
Old 04-21-2008, 09:15 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Don't worry. I will explain better. Indeed, putting a file in cron.hourly should work provided it is executable. I was just asking to you to try another method.

Instead of using the cron.hourly, cron.daily, cron.weekly, cron.monthly system controlled by the /etc/crontab file, you can schedule jobs on a per-user basis. Each user on your system has the possibility to define and mantain his own crontab. The command to do this is
Code:
crontab -e
and the resulting crontab will be automatically written in the /var/spool/cron directory and will be called as the user name owning the crontab.

So if your script is /root/script.sh or /root/pippo or /path/to/your/script/name_of_the_script (that is regardless its name and extension) you can edit your crontab (I assume you're root for this test) and put the line
Code:
0 * * * * /root/pippo
this will execute the script /root/pippo at 0 minutes of each hour of each day of the year. If this doesn't work, we must investigate the problem further, otherwise there is some mis-configuration in the /etc/crontab system.
 
Old 04-22-2008, 08:21 AM   #12
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
My script file called MoveCSVFile is executable. I verified that this morning again. My file doesn't have an extension to it. Does it need one, like a .sh extenstion on it?

So, basically you are asking me to move my current file from the cron.hourly directory to the /root directory, then edit the crontab with vi to add the command:

0 * * * * /root/MoveCSVFile

to it. Then see if it runs like that or not, correct?
 
Old 04-22-2008, 08:26 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by scstech View Post
My file doesn't have an extension to it. Does it need one, like a .sh extenstion on it?
No. In Unix/Linux systems, file extensions have no special meaning. I think their only purpose is to better recognize the file type (from the user's eyes point of view).

Quote:
So, basically you are asking me to move my current file from the cron.hourly directory to the /root directory, then edit the crontab with vi to add the command:

0 * * * * /root/MoveCSVFile

to it. Then see if it runs like that or not, correct?
Yes, if you don't mind.
 
Old 04-22-2008, 09:06 AM   #14
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I moved my script file in the /root directory and edit the crontab to add the command you gave me. I will wait and see if the files move like their suppose to do.
 
Old 04-22-2008, 10:32 AM   #15
scstech
LQ Newbie
 
Registered: Apr 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Ok, the files moved. What should I do now to troubleshoot why it won't work the other way of just placing the file in the cron.hourly directory? What is the difference in how I was doing to editing the crontab file and adding that line in it? Should I have done that before to show where the file is in the cron.hourly directory?

Oh, and when it ran, it did create a file by the root user name, but it was in the tab folder under the /var/spool/cron directory, if that makes any difference.

Last edited by scstech; 04-22-2008 at 11:01 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 01:01 PM
move files without overwriting files nathan2225 Linux - General 1 04-11-2008 05:01 PM
How do I move files without overwrite exitng files? nadavvin Linux - General 10 11-03-2006 03:49 PM
using crontab to back up files tomolesonjr Linux - Newbie 1 04-27-2006 07:15 PM
crontab & quota files are temporary files? hamish Linux - Software 0 07-10-2004 03:09 PM


All times are GMT -5. The time now is 12:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration