Running any of the commands varnishstat, varnishhist, varnishlog, varnishtop will give the error message as
Could not get hold of varnishd, is it running?
Below is some information for understanding.
sudo service varnish status
varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2017-11-15 15:28:04 UTC; 1h 16min ago
Process: 10235 ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m (code=ex
Main PID: 10251 (varnishd)
Tasks: 217
Memory: 106.2M
CPU: 1.673s
CGroup: /system.slice/varnish.service
├─10251 /usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
└─10271 /usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Nov 15 15:28:03 varnish1 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Nov 15 15:28:04 varnish1 varnishd[10235]: Debug: Platform: Linux,4.11.0-1014-azure,x86_64,-junix,-smalloc,-smalloc,-hcritbit
Nov 15 15:28:04 varnish1 varnishd[10251]: Platform: Linux,4.11.0-1014-azure,x86_64,-junix,-smalloc,-smalloc,-hcritbit
Nov 15 15:28:04 varnish1 varnishd[10235]: Debug: Child (10271) Started
Nov 15 15:28:04 varnish1 varnishd[10251]: Child (10271) Started
Nov 15 15:28:04 varnish1 varnishd[10251]: Child (10271) said Child starts
Nov 15 15:28:04 varnish1 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
sudo service varnishd status
varnishd.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
sudo service varnishd start
Failed to start varnishd.service: Unit varnishd.service not found.
varnishd -V
varnishd (varnish-5.2.1 revision 67e562482)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2015 Varnish Software AS
Answer:
sudo varnishstat
sudo varnishhist
sudo varnishlog
sudo varnishtop
If you’re using Varnish utilities as unprivileged user, you’re probably missing privileges to execute some actions. For some distros, it’s enough to add yourself to Varnish’s group:
gpasswd -a my_user varnish
Then put the new group in use and at least varnishstat should work.
If sudo varnishstat doesn't help: check if varnish is started
sudo /etc/init.d/varnish start
Following worked fine for me:
sudo varnishadm
Found this thread, however the answers here did not help.
As per: http://lists.varnish-cache.org/pipermail/varnish-misc/2020-July/026914.html when you have updated the serverhostname, you need a full server reboot.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 11 months ago.
Improve this question
I would like to install the most recent tinyproxy version 1.11.0 on Ubuntu. How can I do that?
Unfortunetly does sudo apt update; sudo apt install tinyproxy -y; not work for me and only gives me the most recent stable version 1.10.0 from 2018.
So I tried to download and unzip the most recent version on GitHub
https://github.com/tinyproxy/tinyproxy/releases
But I couldn't get it to work properly installed yet.
What I tried so far:
sudo apt update;
... [loading the unzipped tinyproxy 1.11.0 folder via own GitHub repo]
cd OwnGitHubRepo/tinyproxy-1.11.0
sudo apt-get install build-essential -y
sudo apt install autoconf -y
autoconf
./configure
make
sudo make install
The installation seems to have been working somehow as tinyproxy -v returns: tinyproxy 1.11.0
But I now can not run following command which I would usually do:
sudo /etc/init.d/tinyproxy restart;
As there is no tinyproxy folder inside /etc/ listed yet. :/
(Also sudo lsof -i:8888 doesn't show any listing server yet)
Documentations:
http://tinyproxy.github.io/
Tried under AWS EC2 Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
Make sure you update the system with the latest packages
ubuntu#ip-172-30-2-209:~$ sudo su -
root#ip-172-30-2-209:~#
root#ip-172-30-2-209:~# apt-get update
root#ip-172-30-2-209:~# apt-get dist-upgrade
You can reboot the server if a new kernel is available
root#ip-172-30-2-209:~# reboot
Install the build tools:
root#ip-172-30-2-209:~# apt-get install build-essential
root#ip-172-30-2-209:~# apt-get install autoconf
Download the source code and build the software
root#ip-172-30-2-209:~# wget https://github.com/tinyproxy/tinyproxy/releases/download/1.11.0/tinyproxy-1.11.0.tar.bz2
root#ip-172-30-2-209:~# tar -jxvf tinyproxy-1.11.0.tar.bz2
root#ip-172-30-2-209:~# cd tinyproxy-1.11.0/
root#ip-172-30-2-209:~/tinyproxy-1.11.0# ./configure
root#ip-172-30-2-209:~/tinyproxy-1.11.0# make
root#ip-172-30-2-209:~/tinyproxy-1.11.0# make install
Important directories/files to track:
/usr/bin/mkdir -p '/usr/local/bin'
/usr/bin/install -c tinyproxy '/usr/local/bin'
/usr/bin/mkdir -p '/usr/local/share/tinyproxy'
/usr/bin/install -c -m 644 debug.html default.html stats.html '/usr/local/share/tinyproxy'
/usr/bin/mkdir -p '/usr/local/etc/tinyproxy'
/usr/bin/install -c -m 644 tinyproxy.conf '/usr/local/etc/tinyproxy'
/usr/bin/mkdir -p '/usr/local/share/man/man5'
/usr/bin/install -c -m 644 tinyproxy.conf.5 '/usr/local/share/man/man5'
/usr/bin/mkdir -p '/usr/local/share/man/man8'
/usr/bin/install -c -m 644 tinyproxy.8 '/usr/local/share/man/man8'
/usr/bin/mkdir -p '/usr/local/share/doc/tinyproxy'
/usr/bin/install -c -m 644 AUTHORS NEWS README README.md '/usr/local/share/doc/tinyproxy'
Configure the software
Symlink the configuration folder
root#ip-172-30-2-209:~/tinyproxy-1.11.0# ln -s /usr/local/etc/tinyproxy /etc/
root#ip-172-30-2-209:~/tinyproxy-1.11.0# ls -la /etc/ | grep tinyproxy
lrwxrwxrwx 1 root root 24 Apr 3 09:53 tinyproxy -> /usr/local/etc/tinyproxy
Check the nobody user group
root#ip-172-30-2-209:~/tinyproxy-1.11.0# cat /etc/passwd | grep nobody
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
root#ip-172-30-2-209:~/tinyproxy-1.11.0# cat /etc/group | grep 65534
nogroup:x:65534:
Since nobody is assigned to the nogroup group, change the Group element in tinyproxy config from nobody group to nogroup
root#ip-172-30-2-209:~/tinyproxy-1.11.0# sed -i s/'Group nobody'/'Group nogroup'/g /etc/tinyproxy/tinyproxy.conf
Configure the rest of tinyproxy to your expectations (here is the minimal setup)
root#ip-172-30-2-209:~/tinyproxy-1.11.0# cat /etc/tinyproxy/tinyproxy.conf | grep -v "^#\|^$"
User nobody
Group nogroup
Port 8888
Timeout 600
DefaultErrorFile "/usr/local/share/tinyproxy/default.html"
StatFile "/usr/local/share/tinyproxy/stats.html"
LogLevel Info
MaxClients 100
Allow 127.0.0.1
Allow ::1
ViaProxyName "tinyproxy"
Test the connection
root#ip-172-30-2-209:~/tinyproxy-1.11.0# tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf
NOTICE Apr 03 09:56:13.294 [8179]: Initializing tinyproxy ...
NOTICE Apr 03 09:56:13.294 [8179]: Reloading config file
INFO Apr 03 09:56:13.294 [8179]: Setting "Via" header to 'tinyproxy'
NOTICE Apr 03 09:56:13.294 [8179]: Reloading config file finished
INFO Apr 03 09:56:13.294 [8179]: listen_sock called with addr = '(NULL)'
INFO Apr 03 09:56:13.294 [8179]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
INFO Apr 03 09:56:13.294 [8179]: listening on fd [3]
INFO Apr 03 09:56:13.294 [8179]: trying to listen on host[::], family[10], socktype[1], proto[6]
INFO Apr 03 09:56:13.294 [8179]: listening on fd [4]
INFO Apr 03 09:56:13.294 [8179]: Now running as group "nogroup".
INFO Apr 03 09:56:13.294 [8179]: Now running as user "nobody".
INFO Apr 03 09:56:13.294 [8179]: Setting the various signals.
INFO Apr 03 09:56:13.294 [8179]: Starting main loop. Accepting connections.
CTRL+C
Create a service to run tinyproxy
root#ip-172-30-2-209:~/tinyproxy-1.11.0# nano /lib/systemd/system/tinyproxy.service
[Unit]
Description=tinyproxy service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=nobody
Group=nogroup
ExecStart=/usr/local/bin/tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf
[Install]
WantedBy=multi-user.target
Start the service and check if everything is working fine
root#ip-172-30-2-209:~/tinyproxy-1.11.0# systemctl enable tinyproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/tinyproxy.service → /lib/systemd/system/tinyproxy.service.
root#ip-172-30-2-209:~/tinyproxy-1.11.0# systemctl status tinyproxy.service
● tinyproxy.service - tinyproxy service
Loaded: loaded (/lib/systemd/system/tinyproxy.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-04-03 10:00:20 UTC; 5s ago
Main PID: 8236 (tinyproxy)
Tasks: 1 (limit: 1145)
Memory: 1.0M
CGroup: /system.slice/tinyproxy.service
└─8236 /usr/local/bin/tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: Setting "Via" header to 'tinyproxy'
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: NOTICE Apr 03 10:00:20.980 [8236]: Reloading config file finished
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: listen_sock called with addr = '(NULL)'
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: listening on fd [3]
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: trying to listen on host[::], family[10], socktype[1], proto[6]
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: listening on fd [4]
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: Not running as root, so not changing UID/GID.
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: Setting the various signals.
Apr 03 10:00:20 ip-172-30-2-209 tinyproxy[8236]: INFO Apr 03 10:00:20.980 [8236]: Starting main loop. Accepting connections.
Install net-tools to check the listening ports
root#ip-172-30-2-209:~/tinyproxy-1.11.0# apt-get install net-tools
root#ip-172-30-2-209:~/tinyproxy-1.11.0# netstat -napt | grep LISTEN | grep tinyproxy
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 8236/tinyproxy
tcp6 0 0 :::8888 :::* LISTEN 8236/tinyproxy
root#ip-172-30-2-209:~/tinyproxy-1.11.0# lsof -i:8888
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tinyproxy 8236 nobody 3u IPv4 34119 0t0 TCP *:8888 (LISTEN)
tinyproxy 8236 nobody 4u IPv6 34120 0t0 TCP *:8888 (LISTEN)
Extras
a) Logging and Pid file
Prepare pid and log directories
root#ip-172-30-2-209:~/tinyproxy-1.11.0# mkdir /var/run/tinyproxy/
root#ip-172-30-2-209:~/tinyproxy-1.11.0# mkdir /var/log/tinyproxy/
root#ip-172-30-2-209:~/tinyproxy-1.11.0# chown -R nobody:nogroup /var/run/tinyproxy/
root#ip-172-30-2-209:~/tinyproxy-1.11.0# chown -R nobody:nogroup /var/log/tinyproxy/
Add pid and log parameters into tinyproxy config
root#ip-172-30-2-209:~/tinyproxy-1.11.0#
root#ip-172-30-2-209:~/tinyproxy-1.11.0# cat /etc/tinyproxy/tinyproxy.conf | grep ^LogFile
LogFile "/var/log/tinyproxy/tinyproxy.log"
root#ip-172-30-2-209:~/tinyproxy-1.11.0# cat /etc/tinyproxy/tinyproxy.conf | grep ^PidFile
PidFile "/var/run/tinyproxy/tinyproxy.pid"
Restart service
root#ip-172-30-2-209:~/tinyproxy-1.11.0# systemctl restart tinyproxy.service
b) Process limits
root#ip-172-30-2-209:~# ps aux | grep tinyproxy
nobody 64427 9.8 0.5 789836 10232 ? Ssl 12:11 0:00 /usr/local/bin/tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf
root 65436 0.0 0.0 7696 644 pts/0 S+ 12:11 0:00 grep --color=auto tinyproxy
root#ip-172-30-2-209:~# cat /proc/64427/limits | grep "Max processes\|Max open files"
Max processes 7151 7151 processes
Max open files 1024 524288 files
To bump up those, edit the /lib/systemd/system/tinyproxy.service file and add the following lines in the [Service] section
[Service]
...
LimitNOFILE=700000
LimitNPROC=700000
...
Then reload systemd and restart the service
root#ip-172-30-2-209:~# systemctl daemon-reload
root#ip-172-30-2-209:~# systemctl restart tinyproxy.service
Check the results
root#ip-172-30-2-209:~# ps axu | grep tinyproxy
nobody 68439 0.6 0.1 533092 3068 ? Ssl 12:13 0:00 /usr/local/bin/tinyproxy -d -c /etc/tinyproxy/tinyproxy.conf
root 68490 0.0 0.0 7696 656 pts/0 S+ 12:13 0:00 grep --color=auto tinyproxy
root#ip-172-30-2-209:~# cat /proc/68439/limits | grep "Max processes\|Max open files"
Max processes 700000 700000 processes
Max open files 700000 700000 files
like using systemctl is-active <servicename> gives is if service is active or failed then is it possible to get the uptime or downtime of the systemd service all using systemctl ?
time systemctl status kdm|cat
* kdm.service - LSB: X display manager for KDE
Loaded: loaded (/etc/init.d/kdm; generated; vendor preset: enabled)
Active: active (exited) since Wed 2017-06-21 12:03:26 CEST; 1h 42min ago
Docs: man:systemd-sysv-generator(8)
Tasks: 0 (limit: 4915)
CGroup: /system.slice/kdm.service
is it possible to get the 1h 42min using systemctl
Using GNU grep with options -P and -o
systemctl status kdm | grep -Po ".*; \K(.*)(?= ago)"
Using systemctl's property WatchdogTimestamp
blr8-100-208-Vin:~ # systemctl show novell-nss --property WatchdogTimestamp
WatchdogTimestamp=Tue 2020-11-24 17:30:20 IST
I'm trying to run a bash script on startup as a systemd service, I'm doing this on a Raspberry Pi 4 with Raspbian Buster Lite.
I'm able to execute the bash script if I run it manually ./hls.sh and I am able to also run the service if I do sudo service tv start but the tv.service does not appear to be able to execute the bash script hls.sh on start. I did give permissions chmod 777 for both the service and the bash file as well.
Any help here would be appreciated, I've been trying to figure this out on and off for a month now.
Edit:
Using the suggestions by Carl, I modified the files. However, it still doesn't work. I noticed also that when Type=oneshot you can't do a Restart=always, and if I do a restorecon -r there's an error that says the command isn't found. Per Carl's suggestion I put everything in /opt I decided not to use the temp file suggestion because I need to have the ffmpeg output go to the same place every time (my understanding is that the other way would randomly generate a folder?).
Edit2: Issue was related to this systemd issue: https://unix.stackexchange.com/questions/209832/debian-systemd-network-online-target-not-working the workaround was simply to do RestartSec=5s under [Service]
Bash Script [Before]
#!/bin/bash
/usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin \
-hide_banner -loglevel fatal \
-i http://10.0.0.11:9981/stream/channelnumber/2 \
-vcodec copy -acodec copy -scodec copy -g 60 \
-fflags +genpts -user_agent HLS_delayer \
-metadata service_provider="TimeShift" \
-metadata service_name="TV 1" \
-f hls -hls_flags delete_segments \
-hls_time 60 \
-hls_list_size 480 \
-hls_wrap 481 \
-hls_segment_filename /home/pi/hls/1_%03d.ts /home/pi/hls/1_hls.m3u8
Bash Script [After]
#!/usr/bin/env bash
set -e -o pipefail # return exit code of command with non-zero exit code and stop executing
echo "The solution works!"
/usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin \
-hide_banner -loglevel fatal \
-i http://10.0.0.11:9981/stream/channelnumber/2 \
-vcodec copy -acodec copy -scodec copy -g 60 \
-fflags +genpts -user_agent HLS_delayer \
-metadata service_provider="TimeShift" \
-metadata service_name="JSTV 1" \
-f hls -hls_flags delete_segments \
-hls_time 60 \
-hls_list_size 480 \
-hls_wrap 481 \
-hls_segment_filename /opt/hls/tmp/1_%03d.ts /opt/hls/tmp/1_hls.m3u8
Service [Before]
[Unit]
Description=Timeshift TV
After=tvheadend.service
PartOf=tvheadend.service
Restart=always
[Service]
ExecStartPre=/bin/mkdir -p /home/pi/hls/
ExecStart=/home/pi/hls.sh 103 &
ExecStop=/bin/rm -rf /home/pi/hls
[Install]
WantedBy=default.target
Service [After]
[Unit]
Description=Timeshift TV
After=tvheadend.service
PartOf=tvheadend.service
[Service]
WorkingDirectory=/opt/hls
User=nobody
Type=oneshot
ExecStartPre=+/bin/mkdir -p -m777 /opt/hls/tmp
ExecStart=/opt/hls/hls.sh
ExecStopPost=+/bin/rm -rf /opt/hls/tmp
#Restart=always
[Install]
WantedBy=multi-user.target
After a reboot, checking the status via systemctl status [Before]
● tv.service - Timeshift TV
Loaded: loaded (/etc/systemd/system/tv.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-05-18 05:36:40 BST; 45s ago
Process: 465 ExecStartPre=/bin/mkdir -p -m777 /home/pi/hls/ (code=exited, status=0/SUCCESS)
Process: 472 ExecStart=/home/pi/hls.sh 103 & (code=exited, status=1/FAILURE)
Main PID: 472 (code=exited, status=1/FAILURE)
May 18 05:36:39 tv3 systemd[1]: Starting Timeshift TV...
May 18 05:36:39 tv3 systemd[1]: Started Timeshift TV.
May 18 05:36:40 tv3 systemd[1]: tv.service: Main process exited, code=exited, status=1/FAILURE
May 18 05:36:40 tv3 systemd[1]: tv.service: Failed with result 'exit-code'.
After a reboot, checking the status via systemctl status [After]
● tv.service - Timeshift tv
Loaded: loaded (/etc/systemd/system/tv.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-05-18 20:13:39 BST; 15min ago
Process: 464 ExecStartPre=/bin/mkdir -p -m777 /opt/hls/tmp (code=exited, status=0/SUCCESS)
Process: 471 ExecStart=/opt/hls/hls.sh (code=exited, status=1/FAILURE)
Process: 484 ExecStopPost=/bin/rm -rf /opt/hls/tmp (code=exited, status=0/SUCCESS)
Main PID: 471 (code=exited, status=1/FAILURE)
May 18 20:13:38 tv3 systemd[1]: Starting Timeshift tv...
May 18 20:13:38 tv3 hls.sh[471]: The solution works!
May 18 20:13:39 tv3 systemd[1]: tv.service: Main process exited, code=exited, status=1/FAILURE
May 18 20:13:39 tv3 systemd[1]: tv.service: Failed with result 'exit-code'.
May 18 20:13:39 tv3 systemd[1]: Failed to start Timeshift tv.
[After] starting the process manually still works
● tv2.service - Timeshift tv
Loaded: loaded (/etc/systemd/system/tv2.service; enabled; vendor preset: enabled)
Active: activating (start) since Mon 2020-05-18 20:53:13 BST; 17s ago
Process: 865 ExecStartPre=/bin/mkdir -p -m777 /opt/hls/tmp (code=exited, status=0/SUCCESS)
Main PID: 866 (bash)
Tasks: 2 (limit: 4915)
Memory: 15.3M
CGroup: /system.slice/tv2.service
├─866 bash /opt/hls/hls.sh
└─867 /usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin -hide_banner -logle
May 18 20:53:13 tv3 systemd[1]: Starting Timeshift tv...
May 18 20:53:13 tv3 hls.sh[866]: The solution works!
pi#tv3:/etc/systemd/system $ cd /opt/hls
pi#tv3:/opt/hls $ ls
hls.sh tmp
pi#tv3:/opt/hls $ cd tmp
pi#tv3:/opt/hls/tmp $ ls
1_000.ts
There are a few issues with this service file. I'll address them below and then show the final solution.
Avoid putting system-level services under the /home directory. While it may work, it is not advised. There are many out-of-scope issues that could occur by doing that. Instead, consider placing the scripts under /opt, /srv, or /usr/local.
When moving the files around in a big way like this, make sure to run restorecon -r on that directory after moving it. This ensures SELinux contexts are reset to their correct values, rather than mixing the new context with the old context. This can help solve permission denied errors when SELinux is in enforcing mode.
The Restart= directive goes under [Service] not [Unit], so that should be moved there or be removed entirely. It's probably innocuous since systemd is sometimes forgiving, but the systemd.service is the one that defines that directive, not systemd.unit.
Change ExecStart= to not have & at the end. Just leave it be and run it as a normal script. Systemd will handle the rest, if properly configured.
Change ExecStop= to ExecStopPost= instead. This will allow systemd to do its normal stop behavior, which is to send a SIGTERM to the script when being asked to stop the service. After the stop has been completed (either gracefully or forcefully), the ExecStopPost= will run. Since it appears you're using it to cleanup after the script ran, that should be changed. If left as is, systemd will remove the directory then wait for the script to exit, which the script will never do since it wasn't told to, so systemd will wait the timeout period before asking the kernel to SIGKILL the script. Definitely not ideal.
A few extra directives under [Service] would solve some of the assumptions about how this service should behave.
WorkingDirectory= — so that it is defined rather than assumed. If left as default, it becomes the home directory of the User, which if also is left default, would be /root. Instead, set it to the location of the script or to /tmp if PrivateTmp=true.
User= — so that the script doesn't run as root which is the default user. I suggest nobody or whichever user you wish to have managing the ffmpeg execution. This prevents giving the script root privileges when it runs, which ffmpeg shouldn't need.
Type= — this is arguably the most important, since the script is being ran as a one-off and will then exit after succeeding; it doesn't daemonize to the background and continue running as the default simple service would. This should be set to oneshot instead so that systemd knows that; it will treat the script exiting (with a status code of 0) as a successful run of the service, and will move on.
PrivateTmp= — In most cases, this should be enabled. Only in rare circumstances should it be disabled. The default behavior is disabled. When enabled, anything the script tries to store in /tmp will actually be transparently stored in a namespaced directory called /tmp/systemd-private-xxxx-servicename.service-xxxxx/, preventing any conflicts with other files in /tmp. If you use this, I would change your ffmpeg to write to /tmp instead of managing a directory next to the script, enabling you to remove ExecStartPre= and ExecStopPost= altogether.
Make sure the ExecStartPre= and ExecStopPost= have a + prepended, that way they run as root instead of the defined User= which might not be root; this may or may not be necessary depending on your expectations with the directory. See systemd.service Table 1. Special executable prefixes for more info.
Change WantedBy=default.target to multi-user.target; it is more likely that this service is supposed to be ran for that target, rather than whatever target happens to get booted as default.
You probably shouldn't make the script chmod 777, I would go with a modest chmod 755 instead. It should only need the read and execute bits (+rx).
So without further adieu, this is the final solution:
Directory Structure: ls -lZ /opt/sfo-61862759
total 8
-rwxr-xr-x. 1 carl users unconfined_u:object_r:usr_t:s0 58 May 18 01:44 sfo-61862759.bash
-rw-r--r--. 1 carl users unconfined_u:object_r:usr_t:s0 316 May 18 01:53 sfo-61862759.service
Shell Script: /opt/sfo-61862759/sfo-61862759.bash
#!/usr/bin/env bash
set -e -o pipefail # return exit code of command with non-zero exit code and stop executing
echo "The solution works!"
# put ffmpeg stuff here and remove the echo if you choose
Service File: /etc/systemd/system/sfo-61862759.service
[Unit]
Description=Stackoverflow Question #61862759 Solution
[Service]
WorkingDirectory=/tmp
User=nobody
Type=oneshot
#ExecStartPre=+/bin/mkdir -p /opt/sfo-61862759/temp
ExecStart=/opt/sfo-61862759/sfo-61862759.bash
#ExecStopPost=+/bin/rm -rf /opt/sfo-61862759/temp
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Systemd Status Output:
01:53 carl#utility.localdomain:/opt/sfo-61862759$ sudo systemctl start sfo-61862759.service
01:54 carl#utility.localdomain:/opt/sfo-61862759$ sudo systemctl status sfo-61862759.service -n 4
● sfo-61862759.service - Stackoverflow Question #61862759 Solution
Loaded: loaded (/opt/sfo-61862759/sfo-61862759.service; linked; vendor preset: disabled)
Active: inactive (dead)
May 18 01:54:02 utility.localdomain systemd[1]: Starting Stackoverflow Question #61862759 Solution...
May 18 01:54:03 utility.localdomain sfo-61862759.bash[459746]: The solution works!
May 18 01:54:03 utility.localdomain systemd[1]: sfo-61862759.service: Succeeded.
May 18 01:54:03 utility.localdomain systemd[1]: Finished Stackoverflow Question #61862759 Solution.
I'm trying to set up a minecraft server to play with my friends. It's my first time doing this on linux, so I have a (I believe) small problem. I can't figure out how to set up it on my usb hard drive.
Here's my minecraft.service
GNU nano 2.9.3 minecraft.service
[Unit]
Description=Minecraft Server
After=network.target
[Service]
WorkingDirectory=/media/main/ALL/.minecraft-server
User=minecraft
Restart=always
ExecStart=/usr/bin/screen -DmS mc-%i /usr/bin/java -Xms512M -Xmx3584M -jar -DIReallyKnowWhatIAmDoingISwear spigot*.jar nogui
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 15 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'
[Install]
WantedBy=multi-user.target
small edit - I removed -%i, because it's to make multi-server I believe
and here is status of service after start
● minecraft.service - Minecraft Server
Loaded: loaded (/etc/systemd/system/minecraft.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2019-10-19 11:30:25 CEST; 17min ago
Process: 1462 ExecStart=/usr/bin/screen -DmS mc- /usr/bin/java -Xms512M -Xmx3584M -jar -DIReallyKnowWhatIAmDoingISwear spigot*.jar nogui (code=exited, status=1/FAILURE)
Main PID: 1462 (code=exited, status=1/FAILURE)
Oct 19 11:30:25 ubuntu systemd[1]: minecraft.service: Service hold-off time over, scheduling restart.
Oct 19 11:30:25 ubuntu systemd[1]: minecraft.service: Scheduled restart job, restart counter is at 5.
Oct 19 11:30:25 ubuntu systemd[1]: Stopped Minecraft Server.
Oct 19 11:30:25 ubuntu systemd[1]: minecraft.service: Start request repeated too quickly.
Oct 19 11:30:25 ubuntu systemd[1]: minecraft.service: Failed with result 'exit-code'.
Oct 19 11:30:25 ubuntu systemd[1]: Failed to start Minecraft Server.
user was created by "useradd minecraft" with no password
Distro is Ubuntu Server 18.04.3 LTS
I need it on usb hard drive because I have small sd card. In future I want to replace it with ssd on adpter
Ok. Thank you all for not helping me. After hours I found solution(s).
I re-created user minecraft with --system
Created group minecraft with --system
added user to group
Set home folder of minecraft user to .../.minecraft-server (when i did it on create it throwed error)
This fixed my problem with server not starting at all, but then i cannot attach to screen.
I tried -c with custom config and editing /etc/screenrc (second option totally broke screen, so I needed to reinstall it). Finally i created .screenrc in /.minecraft-server with options multiuser on and added users that can attach.
Now it's working, but I spend about 10 hours straight to fix it...
Creating user and group:
adduser --system minecraft
addgroup --system minecraft
usermod -a -G minecraft minecraft
usermod --home /home_folder minecraft
.screenrc in minecraft's home directory:
multiuser on
addacl root
addacl user1
Finall version of minecraft.service in systemd:
[Unit]
Description=Minecraft Server
After=network.target
[Service]
WorkingDirectory=/media/main/ALL/.minecraft-server
User=minecraft
Group=minecraft
Type=forking
Restart=on-failure
#RestartSec=20 5
ExecStart=/usr/bin/screen -dmS mc /usr/bin/java -Xms512M -Xmx3584M -jar -DIReallyKnowWhatIAmDoingISwear spigot-1.14.4.jar nogui
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "say SERVER SHUTTING DOWN IN 15 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc -X eval 'stuff "stop"\015'
[Install]
WantedBy=multi-user.target
To attach to screen: screen -x minecraft/mc
where minecraft is user, and mc is screen name
I installed httpd 2.4.27 in CentOS 7. I configured httpd.service as systemd service. But everytime I got this error PID file /usr/local/apache2/logs/httpd.pid not readable (yet?) after start. when I start or restart httpd.service.
Here is my configuration of httpd.service:
[root#localhost ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -k start
ExecReload=/usr/local/apache2/bin/apachectl -k graceful
ExecStop=/usr/local/apache2/bin/apachectl -k graceful-stop
PIDFile=/usr/local/apache2/logs/httpd.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root#localhost ~]#
Here is the error info:
[root#localhost ~]# systemctl start httpd.service
[root#localhost ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2017-09-01 22:30:48 EDT; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 2173 ExecStart=/usr/local/apache2/bin/apachectl -k start (code=exited, status=0/SUCCESS)
Main PID: 2176 (httpd)
CGroup: /system.slice/httpd.service
├─2176 /usr/local/apache2/bin/httpd -k start
├─2177 /usr/local/apache2/bin/httpd -k start
├─2178 /usr/local/apache2/bin/httpd -k start
└─2179 /usr/local/apache2/bin/httpd -k start
Sep 01 22:30:47 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Sep 01 22:30:48 localhost.localdomain systemd[1]: PID file /usr/local/apache2/logs/httpd.pid not readable (yet?) after start.
Sep 01 22:30:48 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
[root#localhost ~]#
Here is the permission of httpd.pid file:
[root#localhost ~]# ll /usr/local/apache2/logs/
total 8
-rw-r--r--. 1 root root 0 Sep 1 20:46 access_log
-rw-r--r--. 1 root root 3270 Sep 1 22:30 error_log
-rw-r--r-- 1 root root 5 Sep 1 22:30 httpd.pid
[root#localhost ~]#
I found there was no use to change the permission of httpd.pid file manually, because the file will be removed automatically when service gets stopped. And new httpd.pid file will be generated once service is turned on.
Note: I see some posts saying updating /usr/lib/tmpfiles.d/httpd.conf will work. But I don't have this file since I installed apache from source. I think only 'yum install httpd' has this.
Anyone can help? I just want to have a clean service start with no error or warning. Thanks.
you can set /lib/tmpfiles.d/httpd.conf follow:
d /usr/local/apache2/logs/httpd.pid 700 apache apache
vim /etc/systemd/system/httpd.service
Add parameter of "ExecStartPost" is work,
[Unit]
Description=httpd service
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/usr/local/apache2/logs/httpd.pid
ExecStart=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k start'
ExecStop=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k stop'
ExecReload=/bin/sh -c '/usr/sbin/apachectl -f /etc/httpd/httpd.conf -k graceful'
ExecStartPost=/bin/sh -c '/usr/bin/chown apache:apache -R /usr/local/apache2/logs/httpd.pid'
[Install]
WantedBy=multi-user.target