Home > Back-end >  How do I combine these 2 scripts to run multiple cronjobs using flags?
How do I combine these 2 scripts to run multiple cronjobs using flags?

Time:03-04

I have 2 similar scripts. First one runs everyday once at a specific time and prints the "success" message at that time.

if [[ $status == 200 ]]; then
    echo "success"
else
    echo "error"
fi

Second script runs every 30 minutes and prints nothing unless there's an error.

if [[ $status != 200 ]]; then
    echo "error"
fi

I'm running both using cron jobs.

0 11 * * * /home/username/script1.sh
*/30 * * * * /home/username/script2.sh

I want to know how these 2 scripts can be combined preferably using flags. Basically I want a single script that can send a success message everyday at 9am while also running every 30 minutes without sending that success message again and again and then only sends error message if anything's down.

I did get solution for combining scripts: How to combine these 2 scripts and run multiple cron jobs for the same script to get different outputs? but I'm confused on how should I do it with flags as I'm not much familiar with them.

CodePudding user response:

Add a flag --error-only and check for this in the script.

if [[ $status == 200 ]]; then
    if [[ "$1" != "--error-only" ]]; then
        echo "success"
    fi
else
    echo "error"
fi

Then modify the crontab to pass that flag except for one of the runs.

0 11 * * * /home/username/script1.sh
*/30 0-10,12-23 * * * /home/username/script1.sh --error-only
30 11 * * * /home/username/script1.sh --error-only
  • Related