Creating bash for systemd running background

71 Views Asked by At

I am trying to write a script to log the change in MySQL table: /home/user/trigger_systemd/check_db1.bash

#!/bin/bash
mysqloption='--defaults-extra-file=/home/user/.mycnf/local_root.cnf'

lastUpdate_0=$(mysql $mysqloption -N db1 -e "SELECT DATE(lastUpdate) FROM db1.lastUpdate")
lastUpdate_1=$(mysql $mysqloption -N db1 -e "SELECT DATE(lastUpdate) FROM db1.lastUpdate")

#echo $lastUpdate_0

while :
do
        while [ $lastUpdate_0 == $lastUpdate_1 ]
        do
                lastUpdate_1=$(mysql $mysqloption -N db1 -e "SELECT DATE(lastUpdate) FROM db1.lastupdate")
                #echo $lastUpdate_1
                sleep 20
        done
        echo $(date) ": " $lastUpdate_0 " -> " $lastUpdate_1 >> /home/user/trigger_systemd/trial.log
        lastupdate_0=$lastupdate_1
done

And here is my service script: /etc/systemd/system/trial_monitor.service

[Unit]
Description=monitor trial
After=mysqld.service

[Service]
Type=simple
ExecStart=/home/user/trigger_systemd/check_db1.bash
PIDFile=/tmp/check_db1.pid
#Restart=always

[Install]
WantedBy=multi-user.target

But it keep give me fail result:

● trial_monitor.service - monitor trial
   Loaded: loaded (/etc/systemd/system/trial_monitor.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2023-08-30 09:04:27 HKT; 3s ago
  Process: 2001631 ExecStart=/home/user/trigger_systemd/check_db1.bash (code=exited, status=203/EXEC)
 Main PID: 2001631 (code=exited, status=203/EXEC)

Aug 30 09:04:27 ****.localdomain systemd[1]: Started monitor trial.
Aug 30 09:04:27 ****.localdomain systemd[1]: trial_monitor.service: Main process exited, code=exited, status=203/EXEC
Aug 30 09:04:27 ****.localdomain systemd[1]: trial_monitor.service: Failed with result 'exit-code'.

I have tried to run directly and it success, what's wrong with my writing?

The system runs with SELinux.
If it is the root cause for the fail, how to make it works?

0

There are 0 best solutions below