shell - monitor for file then copy to another directory


Question: 

I'm relatively new to unix scripting, so apologies for the newbie question.

I need to create a script which will permanently run in the background, and monitor for a file to arrive in an FTP landing directory, then copy it to a different directory, and lastly remove the file from the original directory.

The script is running on a Ubuntu box.

The landing directory is /home/vpntest

The file needs to be copied as /etc/ppp/chap-secrets

So far, I've got this

#/home/vpntest/FTP_file_copy.sh
if [ -f vvpn_azure.txt ]; then
cp vvpn_azure.txt /etc/ppp/chap-secrets
rm vvpn_azure.txt
fi

I can run this as root, and it works, but only as a one off (I need it to run permanently in the background, and trigger each time a new file is received in the landing zone.)

If I don't run as root, I get issues with permissions (even if I run it from within the directory /home/vpntest.)

Any help would be much appreciated.




1 Answer: 

Updated: crontab correction and extra info

One way to have a check and move process in background with root permissions, is the "polling" approach done via root user's crontab, with your script.

Steps:

  1. Revise your /home/vpntest/FTP_file_copy.sh:
#!/bin/bash

new_file=/home/vpntest/vvpn_azure.txt

if [ -f "$new_file" ]; then
    mv $new_file /etc/ppp/chap-secrets
fi
  1. Log out. Log in as root user.

  2. Add a cron task to run the script:

    crontab -e

If this is a new machine, and your first time running crontab, you may get a prompt first to choose an editor for crontab, just choose and continue into the editor.

The format is m h dom mon dow command, so if checking every 5 minutes is sufficiently frequent, do:

*/5 * * * * /home/vpntest/FTP_file_copy.sh
  1. Save and close to apply.

It will now automatically run the script every 5 minutes in the background, helping you to move the file if found.

Explanation

Root user, because you mentioned it only worked for you as root.

So we set this in the root user's crontab to have sufficient permissions.

man 5 crontab informs us:

Steps are also permitted after an asterisk, so if you want to say 'every two hours', just use '*/2'.

Thus we write */5 in the first column, which is the minutes column, to set for "every 5 minutes".

FTP_file_copy.sh:

  • uses absolute paths, can run from anywhere
  • re-arranged so one variable new_file can be re-used
  • good practice to enclose any values being checked within your [ ] test
  • uses mv to write over the destination while removing itself from the source directory
 

More Articles


How do I insert PHP variables in a MySQL table?

This question already has an answer here: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers

node.js - Ubuntu + Nodejs + curl: (52) Empty reply from server

I have a nodejs process listening a port on my server:app.get('/statistic', function(req, res) { log_get_connection(req); getStatistic().then(statistic => { res.json(statistic); });});When I try to access it via curl being on the same machine:curl "http://127.0

Unable to Connect to mysql db through php in GCE instance with ubuntu

I'm a little bit frustrated with a problem I'm having in a system migration from AWS to GCE.I've an ec-2 instance running on amazon web services, perfectly working and configured.The problem is that google is offering a 100.000$ credit in a special plan for Startups, my company's CEO asks for a migr


node.js - webpack-dev-server does not create initial bundle on start

I have built a basic babel webpack starter project that i clone from git. The git repository has no dest/output/build folder nor does it have any initial build files. I am knew to webpack so i assumed that when the webpack-dev-server started up it would create a folder (in my case 'build') and do a

mysql - OTRS Fatal Error

hey i want to install otrs on an apache webserver with linux ubuntu.i created a new database with the otrs web installer but then I get the message: Can't open file /opt/otrs/Kernel/Config/Files/ZZZAAuto.pm.20881: Permission denied

c - Whats the difference between the two command lines? (SegFault)

I have written a code that maps to a shared memory location,so that the first program opens a shared memory block and stores some data in it.And the second program reads the shared data.Whats the difference between the two command lines:1. if(argc<2) { printf("USAGE:%s text-to-share\n",argv[0


linux - List out text files from given directory in unix

Writing shell script to sort text file data from given directory with for loop in ubuntu as: echo "Enter directory Name" read dr path= ` find /home/user -name $dr ` for x in ` ls *.txt $path ` do sort $x done echo "------Script finished-------"But this script is giving me er

linux - Segmentation fault due to lack of memory in C

This code gives me segmentation fault about 1/2 of the time:int main(int argc, char **argv) { float test[2619560]; int i; for(i = 0; i < 2619560; i++) test[i] = 1.0f;}I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me ge

Unable to install LAMP (Linux, Apache, Mysql, PHP stack) on Ubuntu 16.04

I am trying to install LAMP server on my Ubuntu desktop. The problem I am facing is related to IP address of my laptop.While Setting Global ServerName to Suppress Syntax Warnings, as the documentation says here, when I am adding a ServerName directive, pointing to my server's public IP address in co

linux - filter out content between two files

I have two files conditions.txtabcdefghlogs.txtefghijklmnopqrstI am expecting output to be:ijklmnopqrstActual output:efghijklijklmnopmnopqrstqrstHere's the code I had worked till nowfunc(){while read conditiondoif [[ $line = $condition ]] ; then:elseecho "$line";done < condition.txt}while read li