0
votes

Strange for me:

I have a bash script (cams_record.sh) which will take care of recordings of my ipcams. This script should be started by systemd (cams_record.service). That service file should take care of the bash script (which should always be running).

So I created a service file, but when I start the systemd service: it starts/runs which is showed by the systemd status check. However when I check the status again then systemd service has been stopped. What might be wrong (f.i. in the service file)?

Below the output of starting cams_record.service and then twice the output of the status:

systemctl start cams_record

systemctl status cams_record
. cams_record.service - record ipcams
   Loaded: loaded (/lib/systemd/system/cams_record.service; enabled; vendor preset: enabled)
   Active: active (running) since wo 2017-11-01 11:03:53 +03; 39ms ago
 Main PID: 22904 (bash)
   CGroup: /system.slice/cams_record.service
           ??22904 /bin/bash /media/USB2/movie/cams/cams_record.sh
           ??22912 /bin/bash /media/USB2/movie/cams/cams_record.sh
           ??22913 ps -ax
           ??22914 grep [f]fmpeg
           ??22915 grep rtsp://192.168.2.21:554/live/ch00_0
           ??22916 grep \-t 3600

nov 01 11:03:53 Shuttle systemd[1]: Started record ipcams.

systemctl status cams_record
. cams_record.service - record ipcams
   Loaded: loaded (/lib/systemd/system/cams_record.service; enabled; vendor preset: enabled)
   Active: inactive (dead) (Result: exit-code) since wo 2017-11-01 11:03:55 +03; 5s ago
  Process: 22988 ExecStart=/bin/bash /media/USB2/movie/cams/cams_record.sh (code=exited, status=1/FAILURE)
 Main PID: 22988 (code=exited, status=1/FAILURE)

nov 01 11:03:55 Shuttle systemd[1]: cams_record.service: Unit entered failed state.
nov 01 11:03:55 Shuttle systemd[1]: cams_record.service: Failed with result 'exit-code'.
nov 01 11:03:55 Shuttle systemd[1]: cams_record.service: Service hold-off time over, scheduling restart.
nov 01 11:03:55 Shuttle systemd[1]: Stopped record ipcams.
nov 01 11:03:55 Shuttle systemd[1]: cams_record.service: Start request repeated too quickly.
nov 01 11:03:55 Shuttle systemd[1]: Failed to start record ipcams.

The content of the used service file is:

[Unit]
Description=record ipcams
After=network.target
Requires=network.target
RequiresMountsFor=/media/USB2

[Service]
Type=forking
WorkingDirectory=/media/USB2/movie/cams
ExecStart=/media/USB2/movie/cams/cams_record.sh
RemainAfterExit=yes
SyslogIdentifier=cams_record

[Install]
WantedBy=multi-user.target
2

2 Answers

0
votes

The reason why your script fails has as much to with the script as it does with systemd.

What conditions cause your script to exit with status code 1?

Add logging to the script to see how far it gets before it dies.

Confirm your script runs fine outside of systemd, then review Why do things behave differently under systemd?.

Also, consider referencing the network-online service rather than network service, if you want to be certain the network is up when your script runs.

0
votes

@Mark Stosberg: I did what you wrote/suggested.

Well, as written, the bash script runs in CLI (outside systemd) and I think in systemd the script does not exit before the end.

I changed in service file: network -> network-online. Tried different things in the service file like a "&" (at (end of) ExecStart): same result unfortunately. Then I added "PIDFile=" and always get a MAINPID error (also when I remove the "&" again).

So, I went back to the service file (see 1st post) and only changed forking into simple. It seems everything is running. What I can not explain is that in the status view of the running service I see: "active exited" and "Main process exited, code=exited, status=1/FAILURE". For me weird, coz "systemctl is-active cams_record.service" gives me: active:

systemctl status cams_record.service
. cams_record.service - record ipcams
   Loaded: loaded (/lib/systemd/system/cams_record.service; enabled; vendor preset: enabled)
   Active: active (exited) (Result: exit-code) since vr 2017-11-03 09:10:10 +03; 44s ago
  Process: 31355 ExecStart=/media/USB2/movie/cams/cams_record.sh (code=exited, status=1/FAILURE)
 Main PID: 31355 (code=exited, status=1/FAILURE)
   CGroup: /system.slice/cams_record.service
           ??31379 ffmpeg -i rtsp://192.168.2.21:554/live/ch00_0 -r 30 -vcodec copy -an -t 1200 -nostats -nostdin /media/USB2/movie/cams/cam1/cam20171103_0910.mp4

systemd[1]: Started record ipcams.
systemd[1]: cams_record.service: Main process exited, code=exited, status=1/FAILURE

So some questions left:

  1. Is it normal that the service is running (Type simple), the script has been executed well (it does start ffmpeg; checked with ps -ax) and still an error in the status view

  2. I always thought when starting a script, type forking is better, but unfortunately I can not get it working.

Later:

In the bash script was a line like: if ! ps -ax | grep [f]fmpeg |grep ; then start ffmpeg in background

I put that part (where running processes of all cams are checked/restarted) in a "while true-loop" and it seems to be working now only with type=simple. Strange, but working (as it seems to be)