You are not logged in.

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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 18:25:00

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

nobody
The Great
Registered: 2015-08-10
Posts: 3,655

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.

Offline

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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

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

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

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

^ wink

systemctl --help | grep restart

Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

nobody
The Great
Registered: 2015-08-10
Posts: 3,655

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.

Offline

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
Website

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

Hi there @nobody 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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

schwim
Member
From: Coastal VA, Murica
Registered: 2015-09-29
Posts: 342
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