LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create a systemctl script to start a php file (https://www.linuxquestions.org/questions/linux-newbie-8/create-a-systemctl-script-to-start-a-php-file-4175606924/)

NotionCommotion 05-29-2017 07:07 PM

Create a systemctl script to start a php file
 
I just figured out how to write a init.d script to create a daemon which runs a php script, and now found I learned the old way. Wonderful...

That being said, my initial experience with systemctl is good, so upward and onward!

Below is what I came up with (with a lot of help) for the old way.

According to /etc/init.d/README
Quote:

Note that traditional init scripts continue to function on a systemd
system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
into a service unit foobar.service during system initialization.
So, as a quick fix, I added my old init.d to my new server, rebooted, and then executed systemctl enable soaptest (/etc/init.d/soaptest), and it was added the old way.

For a starting point, can I backward-engineer the equivalent systemctl script? I tried using systemctl restart soaptest, but no luck. Will doing so not work?


Code:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Test SOAP Simulator
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:    $local_fs $network $named $time $syslog
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start SOAP Simulator
# Description:      Initiate a given PHP file every 5 seconds to simulate a SOAP server.
### END INIT INFO

SCRIPT="/usr/bin/php /var/www/soap/test_soap.php"
RUNAS=Michael
PIDFILE=/var/run/test_soap.pid
LOGFILE=/var/log/test_soap.log
PROG="SOAP Server"

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo "$PROG already running" >&2
    return 1
  fi
  echo "Starting $PROG…" >&2
  #redirct I/O so non-root user doesn't need special write position to log file.
  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  su -c "$CMD" $RUNAS  3>"$LOGFILE" >"$PIDFILE"
  echo "$PROG started" >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo "$PROG not running" >&2
    return 1
  fi
  echo "Stopping $PROG…" >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo "$PROG stopped" >&2
}

status() {
    if [ -f "$PIDFILE" ] && ps -p $(cat $PIDFILE) >/dev/null;
    #if [ -d /proc/$(<$PIDFILE) ];
    then
      echo "$PROG is running"
    else
      echo "$PROG is not running"
    fi
    RETVAL=$?
    return $RETVAL
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
esac


AwesomeMachine 05-30-2017 12:18 AM

This looks like a good place to start: https://fedoramagazine.org/systemd-c...vinit-scripts/

NotionCommotion 05-30-2017 11:29 AM

Quote:

Originally Posted by AwesomeMachine (Post 5716799)
This looks like a good place to start: https://fedoramagazine.org/systemd-c...vinit-scripts/

Thanks. That was helpful.

Do you know if a service unit file was actually automatically created based on my old init.d script? If so, where would it be located?

NotionCommotion 05-30-2017 01:31 PM

A couple of questions.
  1. How do I make the php script run in the background?
  2. I don't wish to run the php script as root, however, do wish it to output to /var/log/soap.log. How can that be accomplished?
Thanks

Code:

[michael@devserver system]$ cat /usr/lib/systemd/system/greenbeand.service
[Unit]
Description=SOAP Server
#Documentation=man:sshd(8) man:sshd_config(5)
After=syslog.target

[Service]
#EnvironmentFile=/etc/soap/config
Type=forking
#User=michael
User=root
#ExecStart=/usr/bin/php /var/www/soap/server.php 1337 0 >&3 2>&1 & echo \$!
ExecStart=/usr/bin/php /var/www/soap/server.php 1337 0 &
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-abort
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target
[michael@devserver system]$



All times are GMT -5. The time now is 04:28 PM.