Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


cron job to scan another machine and move files

Posted on 2006-04-19
Medium Priority
Last Modified: 2013-12-16
Hi Experts.

I am looking for a script that will scan a particualar server (destination) every 10 minutes looking for a .dat file. once it finds the file it should MOVE (instead of copy and then delete) the file to the source machine.

Question by:muthiahmerchant
  • 4
  • 3
  • 2

Expert Comment

ID: 16492905
You cannot directly move a file to another machine: only copy it and  delete the source. But you could move the file to a different directory, and then copy and delete it.
LVL 15

Expert Comment

ID: 16493012
>>You cannot directly move a file to another machine:

This is not true. It depends on how are you going to be accessing that other server. If you are using NFS or Samba to access the file you can just mv it provided you have enough permissions.

To schedule something every 10 minutes, in /etc/crontab

0/10 * * * * root /root/myscript

then create /root/myscript as

mv fromfile tofile

and so on.

If you are using FTP, you can do that by using curl -Q option to send a literal FTP command to delete the file after download.

Author Comment

ID: 16497320
Thanks for the reply,

I am using an sftp server. This server is outside of the firewall and files from this server needs to be moved to a server inside of the firewall.

in the script root/myscript can you tell me how I can connect to a remote machine and then move or copy the file.

really appreciate your help
Thanks again

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 15

Expert Comment

ID: 16497889
You can write your commands in a batch file and use sftp -b. See man sftp for more details.

Expert Comment

ID: 16498076
If you install sshfs you can mount the remote directory over an ssh link and then the cron script is a simple mv command.

See http://fuse.sourceforge.net/sshfs.html

Another option (which achieves the same result) is lufs:


On the local machine (inside the firewall), the remote directory looks like just another directory, but all file and directory accesses go via a ssh login to the remote machine.

I use lufs regularly, I have an entry set up in /etc/fstab like this:

/dev/sshfs /mnt/LOCAL-MOUNT-POINT lufs nosuid,fs=sshfs,host=REMOTE_HOSTNAME,root=REMOTE-DIRECTIRY-PATH,username=REMOTE-USERNAME,user,noauto 0 0

Then as a user (not as root) I can run:


to mount the remote directory. Note that the mount point must be owned by the user.

(Technically, using "mv" on an NFS or Samba mounted filesystem, or even when the source and target directories are on different partitions, does not move the file: it copies and deletes it. You can only do a true "move" within the same local partition).

Author Comment

ID: 16502738
I am using sftp

this is what i did so far

created a file called sweep.txt in /var/spool/cron/crotabs

in this file i put the following

10 * * * * /home/sweep.sh

my sweep.sh looks like this

sftp elo
pass xxx
cd /home/qa
get rulesets.jar
get validation.xml

finally i did crontab sweep.txt

after 10 minutes i didn't get any of those files. does anyone see anything wrong.

LVL 15

Expert Comment

ID: 16503464
ok, here is what's wrong:

to make a file a shell script, you need to:

1. set "execute" permission on it (chmod a+x sweep.sh)
2. specify the shell that will execute it in the first line like:


Second thing that's wrong is that you really should create 2 files, one a shell script that starts

/usr/sbin/sftp -b /full/path/to/mysftpbatchfile

and second the mysftpbatchfile itself.

What you did is you mixed up shell commands (sftp elo) and commands that should be issued in sftp (pass, etc.).

Once you get it done, test your shell script first by running it from the shell, and if it works then schedule it in cron.

Author Comment

ID: 16508065
Thank you so much

I replaced my sweep.sh with the following but i still asks me for password. do you know how i can specify the password in the same line as the user@machine. I tried uesrname/password@machine, but that didn't work

sftp -b /home/mysftpbatchfile.sh root@elo

also does mysftpbatchfile need to end with .sh and have the permissions

thanks for the response
LVL 15

Accepted Solution

m1tk4 earned 2000 total points
ID: 16508734
you need to use public/private key authentication to avoid password prompts. See http://www.tech-recipes.com/openssh_tips354.html for more details.

no, mysftpbatchfile does not need to end with .sh or have the execute permissions.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question