You are not logged in.

#1 2019-03-22 02:01:06

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Need to start/stop/restart a process via a PHP script

Hi there gents!

I run a game server and to date, I've used screen to manage the server process but now there's a couple of people that also need the ability to start/stop/restart the process as well and they don't shell in, so I'd like to add the ability to manage this via the web panel I wrote for the admins.

The process in question(that I run to start the server:

cd /home/game/server-data
../../artifacts/run.sh | tee run.log

so I thought I could create a bash file:

chdir('/home/game/server-data');
$output = shell_exec('../../artifacts/run.sh | tee run.log');

and then figure out how to work with that via PHP but then I started thinking... how do I handle shutting down and restarting that process?  How would a script know which process to kill?

Which is why I'm here.  You guys are the smartest folk I know when it comes to this so I thought I'd ask before moving in any one direction.  If anyone has any ideas, I'd love to hear them.

Thanks for your time!


Schw.im! A social site with an identity crisis.

Offline

#2 2019-03-22 14:55:27

earlybird
ほやほや
Registered: 2015-12-16
Posts: 699
Website

Re: Need to start/stop/restart a process via a PHP script

Interesting problem.

It entirely depends on how you have set the webpanel<>game interface up. If the panel and the game server run under the same user (if they are both unprivileged but the same user), then I'd recommend creating a systemd user service for the game server process so the owning user can simply do systemctl --user restart game-server or similar.

If the user the web panel is running as and the user running the game server process are different OR the web panel process is chrooted and nicely isolated as it should be then you have to pick a system architecture that fits the purpose. The problem then becomes to allow an unprivileged process execute a privileged action within the system while not introducing security problems.

One hacky-wacky solution would be to run the game server process as the game server user but as a system service using the User= unit file attribute (so it stays unprivileged). Then, an old-style sysadmin would perhaps define a SUDOERS command for the exakt action (restart the game server) and allow the web server user (the panel) execute that action as a privileged user.

One better solution that uses as much freedesktop.org ideas as possible would be to allow the web panel user execute an action against the game server user  based on authentication against polkit. You'd allow the web panel user to invoke a specific systemd action with specific parameters (identifying the game server service) over its Dbus interface against the game server service. You'd have to call stuff over dbus.

The classic systemd-less architecture for this would be that you have an unprivileged game server process that is augmented by a supervisor, for example supervisord, which exposes a command and control channel, for example a named pipe or a UNIX socket, access to which is regulated using classic discretionary access control (users and groups). You would then add the web panel user to the C&C group of the supervisor process, thereby allowing it to interface with the C&C channel and tell the supervisor process to restart the game server process. Services that implement this model are for example docker and postfix (but in their case, they implement the C&C channel as part of their software framework – I'm not even sure if supervisord allows what I just described; it's just an example – you could a Bash or PHP program that sets something like this up, IIRC PHP can do sockets :D)

One thing to note is that if you include systemd into the mix (user or system service) you get important things like restart action limiting via StartLimitInterval=! Because if a user of your web panel clicks the button for restarting the game server 6 times in a row (3x doubleclick), things could get problematic (system resources drain, something goes wrong, restart action fails and then your monitoring sends alerts, etc).

To solve your problem securely and elegantly is not trivial and involves a lot of consideration.

Offline

#3 2019-03-22 18:25:00

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

earlybird wrote:

One thing to note is that if you include systemd into the mix (user or system service) you get important things like restart action limiting via StartLimitInterval=! Because if a user of your web panel clicks the button for restarting the game server 6 times in a row (3x doubleclick), things could get problematic (system resources drain, something goes wrong, restart action fails and then your monitoring sends alerts, etc).

Thanks for the great assistance!

Based on what you stated concerning systemd being able to protect against some possible issues with multi-clicking, I'd like to try this route if possible.  Could you tell me how I would get started tackling it in this manner?


Schw.im! A social site with an identity crisis.

Offline

#4 2019-03-26 12:15:36

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Bump in the name of love, before you break my heart.


Schw.im! A social site with an identity crisis.

Offline

#5 2019-03-26 19:58:59

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Need to start/stop/restart a process via a PHP script

schwim wrote:

Based on what you stated concerning systemd being able to protect against some possible issues with multi-clicking, I'd like to try this route if possible.  Could you tell me how I would get started tackling it in this manner?

a general introduction to systemd by reading most of what archwiki has to say about it.
maybe start here:
https://wiki.archlinux.org/index.php/Sy … unit_files
that, and the respective man pages, e.g.

man systemd.service

and web searches. when my query starts with "systemd" i ususally don't even put "linux" in it.

Offline

#6 2019-03-26 20:12:56

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,426

Re: Need to start/stop/restart a process via a PHP script

Schwim, sorry for not being very responsive. I'll see that I write something tomorrow. If only I had a secretary; I could just dictate my thoughts.


Tempus fugit

Offline

#7 2019-04-01 21:31:21

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Alright guys, I've done a lot of reading and this is what I've found by Googling.  I'll post what I'm proposing to try.  I would love it if someone would tell me if I've got it sorted.

cd /etc/systemd/system
nano scotchmain.service

[Unit]
Description=Scotch Server

[Service]
WorkingDirectory=/var/www/clients/client1/web2/home/schwimsandi/fivem/main/server-data
ExecStart=/bin/sh /var/www/clients/client1/web2/home/schwimsandi/fivem/artifacts/run.sh +exec server.cfg
KillMode=process

[Install]
WantedBy=multi-user.target

--enable service
systemctl enable scotchmain.service

--start
service scotchmain start

--stop
service scotchmain stop

--check status
systemctl status scotchmain.service


Schw.im! A social site with an identity crisis.

Offline

#8 2019-04-02 05:24:47

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Need to start/stop/restart a process via a PHP script

that looks fine.
My workflow usually looks like this:

  • systemctl start something

  • systemctl status something; check if everything works as expected, if yes:

  • systemctl enable something

what i wonder:

  • is that Exec line working like that inside a systemd service? It looks complex and i'm not sure what the last bit '+exec ...' means

  • is the working directory directive even necessary?

Offline

#9 2019-04-02 12:53:31

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Thanks for all your help, ohno!

ohnonot wrote:

that looks fine.
My workflow usually looks like this:

  • systemctl start something

  • systemctl status something; check if everything works as expected, if yes:

  • systemctl enable something

Does enable allow it to be used and then start actually starts the service?  Also, is there a single command to restart(stop/start) a resource?

ohnonot wrote:

what i wonder:

  • is that Exec line working like that inside a systemd service? It looks complex and i'm not sure what the last bit '+exec ...' means

  • is the working directory directive even necessary?

Both the working directory and execing the server.cfg are critical to this particular application starting. The server.cfg holds the entire server resource names, license key and more.   If the working directory isn't dictated, it can't be found.

Last edited by schwim (2019-04-02 14:21:19)


Schw.im! A social site with an identity crisis.

Offline

#10 2019-04-02 14:40:48

damo
....moderator....
Registered: 2015-08-20
Posts: 4,971

Re: Need to start/stop/restart a process via a PHP script

^ wink

systemctl --help | grep restart

Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#11 2019-04-04 13:17:29

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Hi there guys,

I'm trying to modify what the service does but I'm having problems figuring out how to do it:

[Service]
WorkingDirectory=/var/www/clients/client1/web2/home/schwimsandi/fivem/main/server-data
*** DELETE FORCE RECURSIVELY /var/www/clients/client1/web2/home/schwimsandi/fivem/main/server-data/cache
*** PAUSE FOR HALF SECOND OR SO
ExecStart=/bin/sh /var/www/clients/client1/web2/home/schwimsandi/fivem/artifacts/run.sh +exec server.cfg
KillMode=process

How would I manage this?


Schw.im! A social site with an identity crisis.

Offline

#12 2019-04-04 18:25:14

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Need to start/stop/restart a process via a PHP script

you write a script that does what you want, and create a systemd service that executes it at the time & frequency you want.

so, afaiu, you want a wrapper script around /var/www/clients/client1/web2/home/schwimsandi/fivem/artifacts/run.sh +exec server.cfg

Offline

#13 2019-04-04 20:07:19

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,426

Re: Need to start/stop/restart a process via a PHP script

Sorry for not contributing here; I'll have metric tons of freetime after my vacation starts the week after next, till then I'm kind of not in the right state of mind.


Tempus fugit

Offline

#14 2019-04-04 21:46:57

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Hi there twoion and it's A-OK.  Life gets pretty busy smile

Is this syntax correct for a bash file?  I think I googled it correctly but wanted to make sure.

#!/bin/bash

cd /var/www/clients/client1/web2/home/schwimsandi/fivem/main/server-data
rm -rf cache
sleep 5s
./var/www/clients/client1/web2/home/schwimsandi/fivem/artifacts/run.sh +exec server.cfg

But I have another question.  I use screen currently to keep a window open so run.sh will continue to run.  Will this system process continue to run until it's stopped?  Will run.sh know to continue running even though I don't have a window of any type open?


Schw.im! A social site with an identity crisis.

Offline

#15 2019-04-05 04:51:43

ohnonot
...again
Registered: 2015-09-29
Posts: 3,773
Website

Re: Need to start/stop/restart a process via a PHP script

the syntax is correct, although i suspect you want the absolute path

/var/...

and not the relative path

./var/...

i have no experience with screen but i somehow don't see why you would need that when a systemd service will "continue running even though I don't have a window of any type open".

Offline

#16 2019-05-02 12:37:59

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 228
Website

Re: Need to start/stop/restart a process via a PHP script

Hi there guys and very sorry for the long pauses between my posts of confusion.  Life puts things on pause smile

I'm back on this issue at the reqeust of some people and I need a bit more help on my bash script prior to trying the service.

#!/bin/bash

cd /var/www/clients/client1/web2/home/schwimsandi/fivem/main/server-data
rm -rf cache
sleep 5s
/var/www/clients/client1/web2/home/schwimsandi/fivem/artifacts/run.sh +exec server.cfg | tee runlogs/run-19-05-02-12-30-59.log

The only thing I've added is the tee or a logfile.  I need help placing a datetimestring into the file dynamically.   I'd like the stamp to reflect when this file was executed.

Can anyone tell me how I would do that?

----------------------------------------------

Addon question: These files are installed under user schwimsandi .  Under which user should I install this bash file and is there anything I need to do permissions-wise to make schwimsandi's files work via this system service?

Last edited by schwim (2019-05-02 13:35:45)


Schw.im! A social site with an identity crisis.

Offline

Board footer

Powered by FluxBB