Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socat and systemd

Tags:

systemd

socat

I need to run a script, which among many things running socat. Running the script from the command line works fine, now what I want is that this script is run as a service.

This is the script I have:

#!/usr/bin/env sh

set -e

TTY=${AQM_TTY:-/dev/ttyUSB0}

/reg_sesion/create
DESTINOS=(http://127.0.0.1)

LOG_DIR=./logs-aqm

mkdir -p "${LOG_DIR}"

###ADDED####

echo $$ > /var/run/colector.pid

socat -b 115200 ${TTY},echo=0,crnl - |

grep --line-buffered "^rs" |
while read post; do
for destino in ${DESTINOS[@]}; do
wget --post-data="$(echo "${post}" | tr -d "\n")" \
-O /dev/null \
--no-verbose \
--background \
--append-output="${LOG_DIR}/${destino//\/}.log" \
"${destino}/reg_sesion/create"
done

echo "${post}" | tee -a "${LOG_DIR}/aqm.log"
done

And the service file:

[Unit]
Description=colector

[Service]
Type=simple
PIDFile=/var/run/colector.pid

User=root
Group=root

#ExecStart=/root/socat.sh
ExecStart=/bin/sh -c '/root/socat.sh'

[Install]
WantedBy=multi-user.target 

When I start the service, the process starts and ends quickly.

any ideas?

Thanks for your time

like image 643
Gaspacho Avatar asked Sep 08 '25 04:09

Gaspacho


1 Answers

Remove PIDFile= from your service file, and see whether it works.

PIDFile= is mainly for Type=forking, where your startup program would fork a sub-process, so you tell SYSTEMD (via PIDFile) to watch for that process. In case of Type=simple , with your long-running service, SYSTEMD will create a sub-process to start your service, so it knows exactly what the PID is.

like image 72
Sproffer Avatar answered Sep 11 '25 01:09

Sproffer