I have this directory structure at /var/sync:# tree -d /var/sync//var/sync/ sync_bi  replicator => Replicator.php   sync.php  sync_pfizer  replicator => Replicator.php   sync.php replicator.sh sync.shAs you can see on each sync_* directory there is script called sync.php and on each replicator directory there is a script called Replicator.php. I need to run those scripts each night. Replicator.php should run at 00:00 AM and sync.php should run at 12:30 AM. I know the right path here is obviously a cron job. I have made two scripts replicator.sh and sync.sh sharing almost the same code the main difference is the script name. See code below:replicator.sh#! /bin/bashcd $1/usr/bin/php Replicator.phpsync.sh#! /bin/bashcd $1/usr/bin/php sync.phpThen I have added each to cronjob as follow:0 0 * * * /var/sync/replicator.sh /var/sync/sync_bi/replicator/0 30 * * * /var/sync/sync.sh /var/sync/sync_bi/0 2 * * * /var/sync/replicator.sh /var/sync/sync_pfizer/replicator/30 2 * * * /var/sync/sync.sh /var/sync/sync_pfizer/My idea behing all this explanation is to optimize my environment by changing script to just one that allow me to perform all the actions in just one cronjob call and keep less files to maintain. So I did a little research and I have found a helpful topics:By using find some/dir -type f -execdir somecommand {} \; is a proper way to achieve the same thing as I am doing with cdAnd by moving into each directory under root path - in my case /var/sync for D in ./*; do    if [ -d $D ]; then        cd $D        run_something        cd ..    fidoneThis is a pseudo code of what I am talking about (in case is not clear enough):read tree structure at /var/syncforeach (directory in root+path)    if (directory_name is replicator)        cd replicator/        /usr/bin/php replicator.php    else if (directory_name is sync_*)        cd sync_*/        /usr/bin/php sync.phpendforeachI am not so bash expert so I am asking for some help to translate that pseudo code in something functional that allow me to achieve what I am looking for, any advice and/or help?UPDATEPerhaps I am wrong but I am not following at all what you guys are trying to do so here is the order of the events:# Run Replicator at mid day and mid night# this two can run at same time since they are pushing data to different DBs/var/sync/sync_bi/replicator/Replicator.php/var/sync/sync_pfizer/replicator/Replicator.php# Run sync at mid day and mid night# this two should run one first and the second after 30 min since they are # pushing data to the same DBs and second depends on first one/var/sync/sync_bi/sync.php/var/sync/sync_pfizer/sync.phpHaving that info (and sorry for not clear this in first) how your solutions should works? As you have them right now or you need to change anything in order to cover this?
Read each directory and perform actions in Bash script
bash;shell script;cron
The simplest approach would be two find commands:find /var/sync/ -name 'sync.php' -execdir php {} \;find /var/sync/ -name 'Replicator.php' -execdir php {} \;That will look for files called either sync.php or Replicator.php, then cd into their parent directory and execute them with php. You could add the commands directly to your crontab:0 30 * * * find /var/sync/ -name 'sync.php' -execdir php {} \;0 0 * * * find /var/sync/ -name 'Replicator.php' -execdir php {} \;If you need the Replicator scripts to be run with a 30 minute pause between them, you could do something like:find /var/sync/ -name 'Replicator.php' -execdir php {} \; -exec sh -c sleep 30m \;That will first run the script, then wait for 30 minuntes before moving to the next one. Finally, if you need to make sure that i) you never have >1 sync.php running at the same time and ii) each sync script is run after the corresponding replicator script has finished, you can use this as the command you give to cron:find /var/sync/ -name 'Replicator.php' -execdir php {} \; ; find /var/sync/ -name 'sync.php' -execdir php {} \;The above will run each of the Replicator.php scripts first (one after the other, not in parallel) and, once they've finished, will run each of the sync.php scripts. 