15

I have a long command-line to put into a systemd ExecStart entry. I understand I can break a long line into multiple ones by ending each non-final line with a backslash.

However, how can I document the parts with comments? E.g. the following won't work:

ExecStart=/bin/ssh -NT -o ExitOnForwardFailure=yes -o ServerAliveInterval=60 -o ServerAliveCountMax=3 \
# local tunnel for this
    -L 172.16.12.34:10001:localhost:10001 \
# remote tunnel for that
    -R3128:127.0.0.1:3128 \
    someserver

If I remove the lines with '#', it works, but then I lose the documentation. If fine-grain in-place documentation of parts of a long command-line is fundamentally impossible, what are some useful alternatives?

Syncopated
  • 267
  • 1
  • 2
  • 5
  • 2
    Effectively what you're trying to do is to embed comments in the middle of a (long) line. You can't do that. The issue is that in shell syntax comments start with a `#` and run to the end-of-line and there is no other method to terminate a comment and have the shell parse the remainder of that line as valid shell code. – HBruijn Aug 07 '17 at 07:32

3 Answers3

8

I think this is what @sven suggests:

#
# Listen locally on port 10001 and tunnel it to someserver
# Listen on someserver port 3128 and tunnel it to localhost
#
ExecStart=/bin/ssh -NT \
    -o ExitOnForwardFailure=yes \
    -o ServerAliveInterval=60 \
    -o ServerAliveCountMax=3 \
    -L 172.16.12.34:10001:localhost:10001 \
    -R3128:127.0.0.1:3128 \
    someserver

Doesn't look too bad to me.

artfulrobot
  • 2,949
  • 13
  • 36
  • 60
5

Just dont forget \ at the end of lines with comments. Systemd will cut them and not use in command

ExecStart=/bin/ssh -NT \
    -o ExitOnForwardFailure=yes \
    -o ServerAliveInterval=60 \
    -o ServerAliveCountMax=3 \
    # local tunnel for this \
    -L 172.16.12.34:10001:localhost:10001 \
    # remote tunnel for that \
    -R3128:127.0.0.1:3128 \
    someserver
Vadim Fint
  • 151
  • 1
  • 1
0
  1. Write a script instead.
  2. No one is preventing you to write the line and then leave additional comments explaining it.
Sven
  • 98,649
  • 14
  • 180
  • 226
  • 2
    1. Where is a standard place or reasonable place to put a script run by a system service? 2. That's quite unreadable if the command-line is very long, e.g. broken into 10 parts. – Syncopated Aug 08 '17 at 01:57