A
A
Anton Shvets2016-12-16 15:51:04
bash
Anton Shvets, 2016-12-16 15:51:04

How to run a bash script as a daemon in systemd?

There is a script for monitoring the file system
/root/.bin/monitor.sh

inotifywait -e create,delete,modify,move -mrq /tmp/mydir | while read events
do
    echo "$(date +"%m.%d.%Y_%T")   $events"  >> /var/log/files.log
done

/etc/systemd/system/mymonitor.service
[Unit]
Description=Monitoring file systems
DefaultDependencies=no
Wants=network.target
After=local-fs.target network.target systemd-sysctl.service systemd-modules-load.service

[Install]
WantedBy=multi-user.target
WantedBy=network-online.target

[Service]
Type=oneshot
ExecStart=/root/.bin/monitor.sh
ExecStop=
RemainAfterExit=true
TimeoutStartSec=5min

to which systemd writes
Dec 16 15:49:26 systemd[1]: Starting Monitoring file systems...
Dec 16 15:49:26 systemd[1]: monitor.service: Main process exited, code=exited, status=203 /EXEC
Dec 16 15:49:26 systemd[1]: Failed to start Monitoring file systems.
Dec 16 15:49:26 systemd[1]: monitor.service: Unit entered failed state.
Dec 16 15:49:26 systemd[1]: monitor.service: Failed with result 'exit-code'.
I can't figure out how to run it correctly.
If you just execute the script with a command in the console, it works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2016-12-18
@Xuxicheta

Solved a problem.

#!/bin/bash -
#ссылка на интерпретатор обязательна

function monitoring() {
    inotifywait -e create,delete,modify,move -mrq $1 | while read events 
    do
       echo "$(date +"%m.%d.%Y_%T")   $events"
    done
}
DIR="%mydir%"
LOGFILE="%mylogfile%"
echo "Start filesystem monitoring: Directory is $DIR, monitor logfile is $LOGFILE"
monitoring $DIR  >> $LOGFILE &

[Unit]
Description=File system Monitoring -- /root/.bin/monitor.sh
After=local-fs.target systemd-sysctl.service systemd-modules-load.service

[Service]
Type=forking
ExecStart=/root/.bin/monitor.sh
EnvironmentFile=/root/monitor.cfg

[Install]
WantedBy=multi-user.target

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question