1

I installed Shadowsocks-r in my CentOS 7 server, and I want to use systemd to manage it. So I created a file in /etc/systemd/system called shadowsocks-r.service.

shadowsocks-r.service:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start
ExecStop=/etc/init.d/shadowsocks-r stop

[Install]
WantedBy=multi-user.target

But the service couldn't be started correctly. When checking the status of the service, below is what I get:

shadowsocks-r.service - Shadowsocks-R Server
Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Tue 2019-05-14 10:29:46 UTC; 8s ago
Process: 19267 ExecStop=/etc/init.d/shadowsocks-r stop (code=exited, status=0/SUCCESS)
Process: 19234 ExecStart=/etc/init.d/shadowsocks-r start (code=exited, status=0/SUCCESS)
Main PID: 19234 (code=exited, status=0/SUCCESS)

May 14 10:29:46 vultr.guest systemd[1]: Started Shadowsocks-R Server.
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: 2019-05-14 10:29:46 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: 2019-05-14 10:29:46 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: IPv6 support
May 14 10:29:46 vultr.guest shadowsocks-r[19234]: Starting ShadowsocksR success
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: 2019-05-14 10:29:46 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: IPv6 support
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: stopped
May 14 10:29:46 vultr.guest shadowsocks-r[19267]: Stopping ShadowsocksR success

The messages shadowsocks-r is from the start and stop function. From the message it seems both start and stop function were executed. Why this happened and what i should do to correct it? thanks!


Update:

After running systemctl start shadowsocks-r.service I tried running journalctl -xe and this is what I get:

May 14 11:09:01 vultr.guest systemd[1]: Started Shadowsocks-R Server.
-- Subject: Unit shadowsocks-r.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit shadowsocks-r.service has finished starting up.
--
-- The start-up result is done.
May 14 11:09:01 vultr.guest polkitd[1547]: Unregistered Authentication Agent for unix-process:19521:1303198 (system bus name :1.57, object path
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: 2019-05-14 11:09:01 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: 2019-05-14 11:09:01 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: IPv6 support
May 14 11:09:01 vultr.guest shadowsocks-r[19527]: Starting ShadowsocksR success
May 14 11:09:01 vultr.guest shadowsocks-r[19560]: 2019-05-14 11:09:01 INFO     shell.py:74 ShadowsocksR 2.8.2
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: IPv6 support
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: stopped
May 14 11:09:02 vultr.guest shadowsocks-r[19560]: Stopping ShadowsocksR success

------Update------

Now I tried a few more and it's confirmed that the systemctl command actually stopped the process even when I used the command "start". Here is what I tried:

  1. First I start the process by /etc/init.d/shadowsocks-r start

And the terminal returns:

IPv6 support
2019-05-16 08:39:30 INFO     util.py:94 loading libcrypto from libcrypto.so.10
2019-05-16 08:39:30 INFO     shell.py:74 ShadowsocksR 2.8.2
started
Starting ShadowsocksR success
  1. Then I run systemctl start shadowsocks-r.service
  2. Then I run /etc/init.d/shadowsocks-r status, and the result is: ShadowsocksR is stopped

So even when I started the shadowsocks-r process using other method, the systemctl will kill it even when I try to start it.

  1. Running systemctl status shadowsocks-r.service, here is what I get:
    shadowsocks-r.service - Shadowsocks-R Server
    Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Thu 2019-05-16 08:45:10 UTC; 12min ago
    Process: 713 ExecStop=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d stop (code=exited, status=0/SUCCESS)
    Process: 686 ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start (code=exited, status=1/FAILURE)
    Main PID: 686 (code=exited, status=1/FAILURE)

    May 16 08:45:10 vultr.guest server.py[686]: 2019-05-16 08:45:10 INFO     shell.py:74 ShadowsocksR 2.8.2
    May 16 08:45:10 vultr.guest server.py[686]: 2019-05-16 08:45:10 ERROR    daemon.py:75 already started at pid 670
    May 16 08:45:10 vultr.guest server.py[686]: IPv6 support
    May 16 08:45:10 vultr.guest server.py[686]: IPv6 support
    May 16 08:45:10 vultr.guest systemd[1]: shadowsocks-r.service: main process exited, code=exited, status=1/FAILURE
    May 16 08:45:10 vultr.guest server.py[713]: 2019-05-16 08:45:10 INFO     shell.py:74 ShadowsocksR 2.8.2
    May 16 08:45:10 vultr.guest server.py[713]: IPv6 support
    May 16 08:45:10 vultr.guest server.py[713]: stopped
    May 16 08:45:10 vultr.guest systemd[1]: Unit shadowsocks-r.service entered failed state.
    May 16 08:45:10 vultr.guest systemd[1]: shadowsocks-r.service failed.

-------Update --------

Per @Daniel B's suggestion, I removed the ExecStop line in the service file:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start

[Install]
WantedBy=multi-user.target

I first stop the server.py process by /etc/init.d/shadowsocks-r stop and it reuturns ShadowsocksR is stopped

Then I run

systemctl daemon-reload
systemctl start shadowsocks-r.service
systemctl status shadowsocks-r.service

I still get the error message, and it seems the systemctl command couldn't start the process (before it can start then stop the process)

shadowsocks-r.service - Shadowsocks-R Server
   Loaded: loaded (/etc/systemd/system/shadowsocks-r.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2019-05-16 09:40:43 UTC; 29s ago
  Process: 2964 ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json -d start (code=exited, status=0/SUCCESS)
 Main PID: 2964 (code=exited, status=0/SUCCESS)

May 16 09:40:43 vultr.guest systemd[1]: Started Shadowsocks-R Server.
May 16 09:40:43 vultr.guest server.py[2964]: 2019-05-16 09:40:43 INFO     util.py:94 loading libcrypto from libcrypto.so.10
May 16 09:40:43 vultr.guest server.py[2964]: 2019-05-16 09:40:43 INFO     shell.py:74 ShadowsocksR 2.8.2
May 16 09:40:43 vultr.guest server.py[2964]: IPv6 support
6
  • After attempting to start the service, run journalctl -xe, it should give you some extended output. Commented May 14, 2019 at 10:37
  • Thanks Michael, I try running journalctl -xe and I append the result to my original question.
    – Ch3_ong
    Commented May 14, 2019 at 11:10
  • I think your service file is fine, perhaps there is something stopping server.py from launching correctly? Have you manually called your ExecStart command? Commented May 15, 2019 at 0:48
  • No I didn't. I noticed the result lists out two processes (start and stop), which I didn't see when running other systemctl command. I'm not sure if that's normal or if that hints anything.
    – Ch3_ong
    Commented May 15, 2019 at 3:51
  • Hm... it normally stops you from doing this, but did you systemctl daemon-reload after saving the file? Commented May 15, 2019 at 4:54

2 Answers 2

2

You’re not specifying the Type. By default, it is simple, which expects that the service process not exit.

According to the project page, running in the foreground is as simple as not passing -d start.

The complete unit file would look like this:

[Unit]
Description=Shadowsocks-R Server
After=network.target

[Service]
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocks-r/config.json

[Install]
WantedBy=multi-user.target

Systemd will automatically take care of monitoring the process, restarting and stopping it as requested/required.

6
  • I removed the ExecStop line but the problem persisits. I updated my question to reflect the return after I made the change.
    – Ch3_ong
    Commented May 16, 2019 at 9:49
  • @Ch3_ong Please read my answer again, fully. You must change the ExecStart line or you must specify a Type (which has more implications).
    – Daniel B
    Commented May 16, 2019 at 10:53
  • Thanks Daniel! I remove the "-d start" and it worked! Though I don't quite understand how this happened (and I couldn't figure out why running in the foreground is as simple as not passing "-d start"), I appreciate your advice!
    – Ch3_ong
    Commented May 16, 2019 at 11:50
  • How to run a program in the background or foreground is entirely specific to the program. It’s really just a matter of reading the documentation. -d start is probably meant as an abbreviation for daemon start.
    – Daniel B
    Commented May 16, 2019 at 11:53
  • Yes it means to start in the background. I don't understand why I need to remove this specification to make things go right ...
    – Ch3_ong
    Commented May 16, 2019 at 11:56
0

put this on service

RemainAfterExit=true
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 23, 2022 at 5:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .