You are not logged in.

#1 2015-04-07 23:38:27

bond
Member
From: Melbourne, Australia
Registered: 2009-05-05
Posts: 62

pipe input into systemctl edit

Howdy all,

Trying to automate some setup - overriding systemd services;

~$ cat <<-'EOF' | sudo systemctl edit sabnzbd                                                                     
[Service]
Group=media
EOF

Received SIGHUP or SIGTERM

Buffer written to /etc/systemd/system/sabnzbd.service.d/.override.confe61282feb5338872.save
editor failed with error code 1.
Edition of /etc/systemd/system/sabnzbd.service.d/override.conf canceled: temporary file empty

~$ cat /etc/systemd/system/sabnzbd.service.d/.override.confe61282feb5338872.save
[Service] Group=media

any tips for piping input into systemctl edit ?

Offline

#2 2015-04-08 00:08:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: pipe input into systemctl edit

I'm not sure I understand. Why not cat directly to /etc/systemd/system/sabnzbd.service.d/override.conf w/o systemctl?

Offline

#3 2015-04-08 00:24:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: pipe input into systemctl edit

systemctl edit uses $EDITOR (I believe - it does use vim here).  So you are essentially piping into an editor.  Does your editor allow this?  Vim doesn't, as the following also fails:

cat <<EOF | vim
one line
another line
EOF

Most editors are made to edit, not to read input from stdin.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2015-04-08 01:00:53

bond
Member
From: Melbourne, Australia
Registered: 2009-05-05
Posts: 62

Re: pipe input into systemctl edit

advantage of using systemctl is I can do this for multiple services at the same time, and have it create the correct directories

EDITOR=nano

echo -e "[Service]\nGroup=media" | nano -
nano <(echo -e "[Service]\nGroup=media")

works


sudo systemctl edit sabnzbd <(echo -e "[Service]\nGroup=media")

doesn't


https://github.com/systemd/systemd/blob … tl.c#L5761

Last edited by bond (2015-04-08 01:22:33)

Offline

#5 2015-04-08 02:13:38

bond
Member
From: Melbourne, Australia
Registered: 2009-05-05
Posts: 62

Re: pipe input into systemctl edit

~$ echo -e '#!/bin/bash\nnano "$@" -' | sudo tee /usr/bin/nanopipe
~$ sudo chmod +x /usr/bin/nanopipe
~$ export SYSTEMD_EDITOR=nanopipe
~$ echo -e "[Service]\nGroup=media" | sudo systemctl edit sabnzbd
Received SIGHUP or SIGTERM

Buffer written to /etc/systemd/system/sabnzbd.service.d/.override.conf45bf955a37bdebe7.save
editor failed with error code 1.

bah

Offline

#6 2015-04-08 02:33:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: pipe input into systemctl edit

bond wrote:
echo -e "[Service]\nGroup=media" | nano -

works

The "-" option specifies that the editted file should be constructed from stdin - so this is apples and oranges...

$ cat <<EOF | nano somefile
> one line
> another line
> EOF
Received SIGHUP or SIGTERM

Buffer written to somefile.save

$ cat somefile.save 
one line another line

But you can write the same output to many files directly much easier that using systemctl


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2015-04-08 03:04:10

bond
Member
From: Melbourne, Australia
Registered: 2009-05-05
Posts: 62

Re: pipe input into systemctl edit

echo -e "[Service]\nGroup=media" | sudo systemctl edit sabnzbd sonarr couchpotato headphones transmission

is much cleaner than;

sudo mkdir -p /etc/systemd/system/{sabnzbd.service.d,sonarr.service.d,couchpotato.service.d,headphones.service.d,transmission.service.d}
echo -e "[Service]\nGroup=media" | sudo tee /etc/systemd/system/{sabnzbd.service.d,sonarr.service.d,couchpotato.service.d,headphones.service.d,transmission.service.d}/override.conf
sudo systemctl daemon-reload

Offline

#8 2015-04-08 06:20:48

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: pipe input into systemctl edit

echo -e "[Service]\nGroup=media" | sudo SYSTEMD_EDITOR=tee systemctl edit sabnzbd sonarr couchpotato headphones transmission

Offline

#9 2015-04-08 06:24:07

bond
Member
From: Melbourne, Australia
Registered: 2009-05-05
Posts: 62

Re: pipe input into systemctl edit

brilliant!

Offline

Board footer

Powered by FluxBB