Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a systemd Specifier to pass to the command in ExecStart

I've written the following systemd service tcpdumpd.service to kick off a persistent tcpdump recording.

[Unit]
Description=TCPDumpd
After=multi-user.target network.target

[Service]
Type=simple
ExecStart=/usr/sbin/tcpdump -pni eth0 -s65535 -G 3600 -w '/var/log/tcpdump/trace_%Y-%m-%d_%H:%M:%S.pcap' -z gzip
Restart=on-abort

[Install]
WantedBy=multi-user.target

tcpdump allows strftime-placeholders like %H for hour, %M for minute and so on to allow you to create time stamped files.

However, systemd has special specifiers than can be used in it, like (%n, %N, %p, %i, %U, %u, %m, %H, %b, %v) So any of the specifiers that overlap, like %m and %H pass through the information from systemd and don't allow the placeholder to be passed through to tcpdump to make the time stamp.

Does anyone know if there is a way to escape the specifiers in systemd so I can pass the %m and %H through to tcpdump?

like image 759
Ben Sooter Avatar asked Nov 10 '16 16:11

Ben Sooter


1 Answers

I've tried to escape special specifiers like %%m, \%m without luck.

But, if you need the work to be done, here is workaround:

Create file tcpdumpd.environment containing definition of TCPDUMP_FORMAT variable.

TCPDUMP_FORMAT=%Y-%m-%d_%H:%M:%S

Modify tcpdumpd.service: add EnvironmentFile= option to it and replace format string with ${TCPDUMP_FORMAT}.

[Unit]
Description=TCPDumpd
After=multi-user.target network.target

[Service]
Type=simple
EnvironmentFile=tcpdumpd.environment
ExecStart=/usr/sbin/tcpdump -pni eth0 -s65535 -G 3600 -w '/var/log/tcpdump/trace_${TCPDUMP_FORMAT}.pcap' -z gzip
Restart=on-abort

[Install]
WantedBy=multi-user.target
like image 162
alexander Avatar answered Oct 04 '22 03:10

alexander