https://github.com/tankibaj/php-daemon
Permanently run a PHP script as a daemon/service
https://github.com/tankibaj/php-daemon
bash linux php shell
Last synced: 5 months ago
JSON representation
Permanently run a PHP script as a daemon/service
- Host: GitHub
- URL: https://github.com/tankibaj/php-daemon
- Owner: tankibaj
- Created: 2019-08-30T20:15:34.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-30T21:05:57.000Z (about 6 years ago)
- Last Synced: 2025-02-17T21:38:21.682Z (8 months ago)
- Topics: bash, linux, php, shell
- Language: Shell
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## PHP script as a Daemon/Service
Assume we need to permanently run a PHP CLI script, it need to run as a daemon/service. It will support service
start
stop
commands. Okay lets start.Create a daemon/service called:
```shell
sudo touch /etc/init.d/myPHPservice
```Open created daemon/service:
```shell
sudo nano /etc/init.d/myPHPservice
```And paste following code
```sh
#! /bin/sh# Installation
# - Paste these to /etc/init.d/myPHPservice
# - chmod +x this
#
# Starting and stopping
# - Start: service myPHPservice start OR /etc/init.d/myPHPservice start
# - Stop: service myPHPservice stop OR /etc/init.d/myPHPservice stopNAME=myPHPservice
DESC="Daemon for PHP CLI script"
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"DAEMON="/usr/bin/php"
DAEMON_OPTS="/path/to/your/php/script.php"START_OPTS="--start --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} ${DAEMON_OPTS}"
STOP_OPTS="--stop --pidfile ${PIDFILE}"test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting ${DESC}: "
start-stop-daemon $START_OPTS >> $LOGFILE
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon $STOP_OPTS
echo "$NAME."
rm -f $PIDFILE
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon $STOP_OPTS
sleep 1
start-stop-daemon $START_OPTS >> $LOGFILE
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esacexit 0
```Next, make it executable by running
```shell
chmod +x /etc/init.d/myPHPservice
```Change
NAME
,DESCRIPTION
,DAEMON
, andDAEMON_OPTS
to reflect your setup.Once done you can start the service by running
/etc/init.d/myPHPservice
start and stop it by running/etc/init.d/myservice stop
. Restarting is also possible by running/etc/init.d/myservice restart
.Verify the running of the daemon by checking the contents of the created PID file:
cat /var/run/myservice.pid
— It should contain a process ID.## Run PHP without browser
You can run PHP Script from Linux terminal without the need of any browser. Run the PHP file located at
/var/www/html/infophp.php
in Linux Command Line as```bash
php -f /var/www/html/infophp.php | less
```Or
```bash
php -f /var/www/html/infophp.php
```Here option
-f
parse and execute the file that follows the command.## Run PHP executable
You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory.
```bash
echo -e '#!/usr/bin/php\n' > phpscript.php
```Notice we used
#!/usr/bin/php
in the first line of this PHP script as we use to do in shell script(/bin/bash)
. The first line#!/usr/bin/php
tells the Linux Command-Line to parse this script file to PHP Interpreter.Second make it executable as
```shell
chmod 755 phpscript.php
```and run it as,
```shell
./phpscript.php
```