Karthikeyan Raju
Karthikeyan Raju

Reputation: 79

Run Crontab in Linux when a new file created in a directory

I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.

*/5 * * * * php /var/www/html/cron/import-csv.php

Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).

I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/

Upvotes: 1

Views: 1922

Answers (2)

EPB
EPB

Reputation: 4029

There's a couple of ways I can think of off the top of my head:

  1. Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
  2. Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)

And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)

SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php

Upvotes: 1

sulabh chaturvedi
sulabh chaturvedi

Reputation: 3984

One way is you can distribute your tasks is 3 different scripts and chain them in cron.

*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php

&& will make sure that, the next script runs only if the previous ran sucessfully.

Upvotes: 0

Related Questions