Linux script for several cronjob

I have a linux server for my joomla website. I am importing xml feeds (around 200). The component give a tool which list all the cron job commands to automaticaly make the feeds import.

I don’t want to add one by one each cron job.

So my idea is to make one cron job which will call a shell script command. Inside this script I will put all these commands.

Problem1: I have to make the import one by one. So the shell script should look like

/usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=2
wait... until import is finished...
/usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=3 wait... until import is finished... etc...

But I guess we cannot know when import is finished? In this case we will wait a few minutes?

Problem 2: I have 200 feeds to import. If for example I run the cron job once per week and the cron job from previous week is not finished… Then I’ll have 2 cron jobs running and this could use all the ressource of server… And it will be down. So I had the idea to put a line of code which will write a value in a indicator.txt file.

If inside indicator.txt file, there is line: cronjob_finished = true Then cron job can start

If inside indicator.txt file, there is line: cronjob_finished = false Then cron job cannot start

As a resume, the code should look like this:

#!/bin/sh
# This is some secure program that uses security.

# read indicator.txt

if cronjob_finished = false then
    STOP THE SCRIPT
else
    let's start the script
    /usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=2
    wait... until import is finished... OR ... wait a few minutes...
    /usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=3
    wait... until import is finished... OR ... wait a few minutes...
    /usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=4
    wait... until import is finished... OR ... wait a few minutes...
    etc.......
    write in indicator.txt, cronjob_finished = true

fi

Is there any expert who will accept to help me please?

Hi,

I wonder whether something like this will help:

#!/bin/bash

if [ -f /tmp/my.lock ]; then
    echo "RSS feed import is still running"
    exit 1
fi

touch /tmp/my.lock

for i in $( seq 1 200); do
     /usr/bin/php /home/website/public_html/superf/cli/com_cmdealaggregator_import_cron.php --id=$i
     sleep 10
done

rm /tmp/my.lock

In the above code we first check whether a lock file exists. If it does we exit otherwise we create a lock file and run for loop with 200 iterations where each iteration number is used as a id for a each individual import. After each import feed execution we wait 10 seconds. Once we are done with all 200 interations we remove the lock file and exit.

Hope this helps. Otherwise let me know if you need further assistance.

Lubos

Thank you very much for the answer. I will try that.

i have defined two cron jobs in the crontab file using crontab -e

first one will run after every 5 minutes and second one will run after every hour. The script script is different for both but in both script they are redirecting the output in a same file which is name cronoutput.

*/5 * * * * /home/usman/Downloads/5mincron

  • */1 * * * /home/usman/Downloads/hourcron