LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2017, 07:07 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 770

Rep: Reputation: Disabled
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
 
Old 05-30-2017, 12:18 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
This looks like a good place to start: https://fedoramagazine.org/systemd-c...vinit-scripts/
 
Old 05-30-2017, 11:29 AM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 770

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
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?
 
Old 05-30-2017, 01:31 PM   #4
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 770

Original Poster
Rep: Reputation: Disabled
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]$
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
systemctl start,enable service in one command. gctaylor1 Fedora 5 11-24-2018 12:20 PM
systemctl [status|start|stop] MySQL.server doesn't work grob115 Linux - Server 1 10-03-2015 06:47 PM
[SOLVED] What is the purpose of systemctl start named.service? Echopurrs Linux - Newbie 1 07-07-2014 12:43 AM
systemctl start php-fpm.service question satimis Linux - Server 2 11-26-2012 07:25 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration