Jump to content

How to run php script permanently in the background?


Guest

Recommended Posts

Basically I am writing a php based chatbot with the telegram api. I use xampp as a test environment so the script runs through a webserver. For now I always need to refesh the website for the script to run. I want it to run automatically in the background so that if a someone messages the bot he gets immadiately a answer.

I was thinking of an infinite while-loop but then the website just loaded forever. I also heard of cronjobs but I does not allow to run a script every second.

How can I get the script to run continuously in the background of the webserver?

Link to comment
Share on other sites

Code your bot as a system service, as in something that runs on system startup and probably has a dependency or two (such as networking). Doing that also means the system can monitor the process and restart it if it fails. Yes, that means you need to spend a bit of time learning what those are and how they work, though that shouldn't take long.

The only difference between a "normal" service and your bot's service is that the command to run your bot looks like "/usr/bin/php /path/to/your/bot.php".

Link to comment
Share on other sites

For example, using systemd on Ubuntu server I have a small chat server script running as a service.  It's run by systemd by creating a service file like this:

[Unit]
Description=PHP Chat Daemon

[Service]
Type=simple
WorkingDirectory=/var/www/kicken/aoeex.com/content/chat/src/
ExecStart=/usr/bin/php phpchatd.php
Restart=on-failure
SyslogIdentifier=phpchatd

[Install]
WantedBy=default.target

The script itself just sits in a loop waiting for connections. 

Your script would need to be setup to loop doing whatever it does as well.

 

  • Great Answer 1
Link to comment
Share on other sites

On 1/8/2024 at 2:10 PM, oem said:

Basically I am writing a php based chatbot with the telegram api. I use xampp as a test environment so the script runs through a webserver. For now I always need to refesh the website for the script to run. I want it to run automatically in the background so that if a someone messages the bot he gets immadiately a answer.

I was thinking of an infinite while-loop but then the website just loaded forever. I also heard of cronjobs but I does not allow to run a script every second.

How can I get the script to run continuously in the background of the webserver?

You 100% want to write this as a cli application, and do not want to run it as a web application.

Cron used to be the way that people scheduled this type of thing, but with modern linux os's services now run under systemd.  Kicken provided a great example of how to write a systemd unit file.

With that said, you will probably want to read this thread for more information on the format of the files and various capabilities. https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file

I would suggest you start with what kicken provided, and read through the thread to answer questions that might come up, in terms of where to put the file.  Systemd has a lot of complexity to it, compared to the things it was designed to replace.  There are features that take care of issues that used to come up with services like semaphores to prevent race conditions, which used to be left to developers to code around (typically with shell scripts) and systemd now takes care of that problem for you, if you use the correct configuration.  

As you should note from the example Kicken provided, there is "Restart=on-failure" which takes care of the problem of your chatbot dying and will restart it.  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.