How to use the ">" operator in a systemd file? - linux

This is my systemd service file:
[Unit]
Description=Foo
[Service]
WorkingDirectory=/tmp/test
ExecStartPre=/usr/bin/yq -o json config.yaml > config.json
ExecStart=/usr/bin/foo run -c config.json
[Install]
WantedBy=multi-user.target
Everything working if I run ExecStartPre and ExecStart in the shell.
But got error when I run systemctl start foo.service:
Error: open >: no such file or directory
Seems the ">" operator doesn't work as shell in the systemd file.

ExecStartPre doesn't use the shell to execute the command, so you can't use shell operators like redirection.
You'll need to invoke the shell explicitly.
ExecStartPre=bash -c '/usr/bin/yq -o json config.yaml > config.json'

Try:
ExecStartPre=/usr/bin/yq -o json config.yaml | tee -a config.json
or:
ExecStartPre=/bin/bash -c '/usr/bin/yq -o json config.yaml > config.json'

Related

systemd and ExecStart with arguments with parsing errors

I have created a fluidsynth service with this ".service" file:
[Unit]
Description=fluidsynth-ng
After=xsynth-network.service
[Service]
EnvironmentFile=/etc/fluidsynth-ng.conf
ExecStart=/usr/bin/fluidsynth -a $AUDIO_DEVICE -c $AUDIO_BUFFERS -z $BUFFER_SIZE -r $SAMPLE_RATE -s -o shell.port=9988 -m $MIDI_DRIVER -i
Type=simple
User=root
Group=root
PIDFile=/tmp/fluidsynth-ng.pid
[Install]
WantedBy=multi-user.target
The problem is I want to pass port parameter to ExecStart but it doesn't work:
[Unit]
Description=fluidsynth-ng
After=xsynth-network.service
[Service]
EnvironmentFile=/etc/fluidsynth-ng.conf
ExecStart=/usr/bin/fluidsynth -a $AUDIO_DEVICE -c $AUDIO_BUFFERS -z $BUFFER_SIZE -r $SAMPLE_RATE -s -o shell.port=$SERVER_PORT -m $MIDI_DRIVER -i
Type=simple
User=root
Group=root
PIDFile=/tmp/fluidsynth-ng.pid
[Install]
WantedBy=multi-user.target
I think there's a problem with $SERVER_PORT parse, because I get this error: "fluidsynth: error: Failed to bind server socket"
EnvironmentFile looks correct:
AUDIO_DEVICE=jack
SERVER_PORT=9988
SAMPLE_RATE=48000
AUDIO_BUFFERS=2
BUFFER_SIZE=64
MIDI_DRIVER=alsa_seq
Any help?
Launch either ss -tulp or netstat -tulp to check which ports are open and which process is listening on your port 9988, and you will probably see some other instance of your process already listening, so you can't assign the port to a different process.

How to start Greeplum on boot - linux

Trying to start Greenplum on system startup. Please find systemd service file content below.
[Unit]
Description=Greenplum daemon
[Service]
EnvironmentFile=/etc/environment
EnvironmentFile=/etc/default/greenplum
User=gpadmin
Group=gpadmin
Type=simple
ExecStartPre=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh"
#ExecStartPre=/opt/greenplum-db-6-6.11.2/greenplum_path.sh
ExecStart=/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1
Restart=on-failure
RestartSec=5s
PrivateTmp=true
[Install]
WantedBy=multi-user.target
All required environment variables are loaded and required path is sourced still not able to start service. Getting "ImportError: No module named gppylib.mainUtils". After machine is started if I run start command(/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1) it is working fine. I am not able to understand what is going wrong. My current setup has two hosts(master and segment host). I have kept segment node up and trying on master. Any help is much appreciated.
Sourcing greenplum_path.sh in ExecStartPre won't work because the environment isn't preserved between ExecStartPre and ExecStart. I would try something like
ExecStart=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh; gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1.

Sudoer does not use the same environnement for python3

I have a python script1 which i use as a library which i import to my main script.
before using it i checked that it was working fine, i noticed that when i run this script1 with "sudo" then it doesn't find one of library he use.
sudo python3 -W ignore detector.py -d datasets -c MLP predict
By searching a little, I found that "sudo" did not use the same environment as my user account, and that it was necessary to add the argument "-E", and it works well.
sudo -E python3 -W ignore detector.py -d datasets -c MLP predict
the script1 also works well when I run it without "sudo"
python3 -W ignore detector.py -d datasets -c MLP predict
Then i import my script1 into my main script "import script1" and call one of the functions from script1 but get the same error from script1.
the problem is that my main script is managed by systemctl with service with root user and i can't use "-E" in ExecStart:
[Unit]
Description= Uplink manager with IA supervison of LORAWAN data
Wants=network-online.target
After=network-online.target
[Service]
ExecStart= /usr/bin/python3 -u/opt/flask_server/uplink_server/uplink_manager.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=uplink_manager.service
Can you help me ? Thanks.
The library that was missing, was in fact present in the root environment, but the version of the library was not right.

systemd-path service not working

I have added systemd service to monitor a path. But it is not working. I touched a .txt file under /tmp/test/. But it is not kicking in my service. I cant see "/tmp/testlog.txt" getting generated. Is there anything wrong in my service?
myservice.path
[Unit]
Description=Path Exists
[Path]
PathExistsGlob=/tmp/test/*.txt
PathChanged=/tmp/test/
[Install]
WantedBy=multi-user.target
myservice.service
[Unit]
Description=Test
[Service]
ExecStartPre=/bin/sh -c 'mkdir /tmp/test && sleep 60'
ExecStart=/bin/sh -c 'echo "Test Success" >> /tmp/testlog.txt & '
[Install]
WantedBy=multi-user.target
tmp dir:
# ls /tmp/test/
ab.txt
#
What could be the reason for the failure?
That was a timing issue. I added dependency and made this service to start as the very last one. That one solved the issue.

CoreOS - Cloud-Config not saving file

I'm trying to write an "initial" cloud-config file that does a bit of setup before my default Cloud-Config file replaces it and takes over. This is what it looks like, however whenever it runs the "clustersetup.service", it can't find the clustersetup.sh file that was supposed to save. Course if I run this from a terminal it works just fine. What am I doing wrong?
#cloud-config
coreos:
etcd:
addr: $private_ipv4:4001
peer-addr: $private_ipv4:7001
fleet:
public-ip: $private_ipv4
units:
- name: clustersetup.service
command: start
content: |
[Unit]
Description=Cluster Setup
[Service]
ExecStartPre=/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh
ExecStart=/usr/bin/bash ~/clustersetup.sh
ExecStop=/usr/bin/bash
Paths specified by systemd cannot be relative. Try this again specifying the full path /home/core/clustersetup.sh.
In my distribution (ubuntu), bash is in /bin. One thing you could do is:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~/clustersetup.sh'
ExecStart=/bin/bash -c ~/clustersetup.sh'
I think you will get the proper expansion of the ~ when pushing it through the shell. However, ~ will be relative to the process id executing the script (I don't know for certain that is core). If you wanted to be sure, you could:
ExecStartPre=/bin/bash -c '/usr/bin/wget -q http://10.0.2.2:8080/clustersetup.sh -O ~core/clustersetup.sh'
ExecStart=/bin/bash -c ~core/clustersetup.sh'
I haven't tested this. I agree with #Brian in that the explicit path would be a better idea. In general it is best not to get a shell involved with execution.

Resources