How to return different exit code for if and else in bash?

#!/bin/sh
db_time=$(mysql -u root -BNe 'USE monit_sikdai; SELECT MAX(alert_date) FROM alerts')
echo $db_time
# 2024-05-31
system_time=$(date +"%Y-%m-%d %H:%M:%S")
db_timestamp=$(date -d "$db_time" +%s)
system_timestamp=$(date -d "$system_time" +%s)
difference=$((system_timestamp - db_timestamp))
if [ $difference -gt 600 ]; then
    echo "alert"
else
    echo "do nothing"
fi

I want to take this value into monit like this.

 check program myscript with path /root/check_db.sh
       if status != 0 then alert

So, I want to send alert when status not equals to some value(say 0).

So, I want if to generate exit status of not zero whereas else to generate exit status of 0. So, I don't get alert when else is executed whereas, I get alert when if gets executed.

I would also want to wait 10 times till I get the differences > 600 before sending alert.

Not sure I understood...
What's wrong with in your if cond. instead of (echo "alert" ) you had directly ( exit 20 ) or any other value you wished...

Since this is localhost, I am just trying to dry run how the alert will work. You're correct.

You wrote in your title returning different exit code...
An exit code is different than an alert, an alert is a message you put, that you check in the output... and exit code is a stop of execution with a code value for you to know where in your script it stopped

loop up to 10 times with some sort of a delay between iterations,

example:

#!/bin/bash

MAXSLEEP=15
MAXRETRIES=10
iteration=1

while [ $iteration -le $MAXRETRIES ]
do
        echo 'existing code .... '

        echo "iteration $iteration of $MAXRETRIES"

        sleep $((1 + $RANDOM % $MAXSLEEP))
        ((iteration++))
done

this is trivial, you should be more than capable of these type of activities given your declared level of experience.

I mean by monit there are cycles or something in monit. Tried to understand how they work.

I've designed the program in such a way that it returns exit code 1 when if is true, which I'll check with monit.


check program myscript with path /root/check_db.sh
       if status != 0 then then exec "send-alert.sh"

Now, I want to execute this only if status!=0 for 10 cycles.

To be clear: the child process returns an un-named status value with exit.

Its parent process must collect that value immediately, with a command like:

status="${?}"

and you would check it like:

if (( status != 0 )); then send-alert.sh; fi

The reason for collecting the status immediately is that almost all Bash commands set a new status. Even an echo of the value as debug, or the first of multiple tests, will change it.

$ 
$ bash  ## Open a child shell.
<my subshell> $ exit 27  ## Exit with status
exit
$ echo "${?}"
27
$ echo "${?}"
0
$ 

The second echo reports the success of the first echo, not the status of the previous shell as might be expected.

I didn't want to ask a new thread. I wrote this script. But the problem is it queries database every 1 monit cycle. Since the target Alerts don;t go at night as no transactions occur at night, it'll keep alerting and spamming emails.
Is there a clever way to check if a system is functioning correctly or not?

The server is a java web server consisting of wrapper.log. I tried to telnet its ports but I suspect the ports of this server change at restart(Not sure).

Any guidance? I need to find if a module in linux server is up or down.

First I need to define the state UP. I am not sure how do I do it.

There must be a function in monit to withhold/delay notifications...