user3134453
user3134453

Reputation: 233

Running crontab only on one line in a file each time

I'm trying to configure crontab to execute at different times different lines of code inside a file. I basically have a bash script file that starts some java -jar. The problem is that each line should be executed at a different time. I can configure crontab to run the whole script at different times but no the lines to run. Now this is important that the bash file will stay only one file and not broken down to a few files. Thanks!

Upvotes: 1

Views: 976

Answers (1)

jDo
jDo

Reputation: 4010

One way of doing it (via command line arguments passed by cron)

some_script.sh:

if test $1 = 1 ; then

    # echo "1 was entered"
    java -jar some_file.jar

elif test $1 = 2 ; then

    # echo "2 was entered"
    java -jar another_file.jar

fi

crontab example:

* 1 * * * /bin/bash /home/username/some_script.sh 1
* 2 * * * /bin/bash /home/username/some_script.sh 2

Another approach (hour matching done in bash script)

some_script.sh:

hour=$(date +"%H");

if test $hour = 1 ; then

    # echo "the hour is 1";
    java -jar some_file.jar

elif test $hour = 2 ; then

    # echo "the hour is 2";
    java -jar another_file.jar

fi

crontab example:

* 1 * * * /bin/bash /home/username/some_script.sh
* 2 * * * /bin/bash /home/username/some_script.sh

Upvotes: 2

Related Questions