<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="https://dev1galaxy.org/extern.php?action=feed&amp;tid=6854&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Dev1 Galaxy Forum / Journey to create a startup service script]]></title>
		<link>https://dev1galaxy.org/viewtopic.php?id=6854</link>
		<description><![CDATA[The most recent posts in Journey to create a startup service script.]]></description>
		<lastBuildDate>Mon, 23 Sep 2024 14:29:51 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: Journey to create a startup service script]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=52394#p52394</link>
			<description><![CDATA[<p>Never mind me. I just thought I&#039;d leave a reference to the LSB standard on init scripts for future hackers.</p><p><a href="https://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/tocsysinit.html" rel="nofollow">Linux Standard Base Core Specification 4.1 - VIII. System Initialization</a></p>]]></description>
			<author><![CDATA[dummy@example.com (s1mple)]]></author>
			<pubDate>Mon, 23 Sep 2024 14:29:51 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=52394#p52394</guid>
		</item>
		<item>
			<title><![CDATA[Re: Journey to create a startup service script]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=52392#p52392</link>
			<description><![CDATA[<p>Thank you for sharing this.</p>]]></description>
			<author><![CDATA[dummy@example.com (stopAI)]]></author>
			<pubDate>Mon, 23 Sep 2024 11:05:42 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=52392#p52392</guid>
		</item>
		<item>
			<title><![CDATA[Journey to create a startup service script]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=52384#p52384</link>
			<description><![CDATA[<p>I am a novice of sysvinit, but I like its simplicity. However, I don’t understand the script creation syntax of sysvinit, so I started searching github. Finally, I found a tool that can semi-automatically create service scripts. I copied it and performed the following operations:</p><p>1. I created a /etc/inittools directory, which contained the tool create, a template file, and a Readme file for creating service scripts</p><p>The content of create is as follows:</p><div class="codebox"><pre class="vscroll"><code>#!/bin/bash

SERVICE_FILE=$(tempfile)

if [ ! -e service.sh ]; then
  echo &quot;--- Download template ---&quot;
  echo &quot;I&#039;ll now download the service.sh, because is is not downloaded.&quot;
  echo &quot;...&quot;
  wget -q https://raw.githubusercontent.com/wyhasany/sample-service-script/master/service.sh
  if [ &quot;$?&quot; != 0 ]; then
    echo &quot;I could not download the template!&quot;
    echo &quot;You should now download the service.sh file manualy. Run therefore:&quot;
    echo &quot;wget https://raw.githubusercontent.com/wyhasany/sample-service-script/master/service.sh&quot;
    exit 1
  else
    echo &quot;I donloaded the tmplate sucessfully&quot;
    echo &quot;&quot;
  fi
fi

echo &quot;--- Copy template ---&quot;
cp service.sh &quot;$SERVICE_FILE&quot;
chmod +x &quot;$SERVICE_FILE&quot;
echo &quot;&quot;

echo &quot;--- Customize ---&quot;
echo &quot;I&#039;ll now ask you some information to customize script&quot;
echo &quot;Press Ctrl+C anytime to abort.&quot;
echo &quot;Empty values are not accepted.&quot;
echo &quot;&quot;

prompt_token() {
  local VAL=&quot;&quot;
  if [ &quot;$3&quot; = &quot;&quot; ]; then
    while [ &quot;$VAL&quot; = &quot;&quot; ]; do
      echo -n &quot;${2:-$1} : &quot;
      read VAL
      if [ &quot;$VAL&quot; = &quot;&quot; ]; then
        echo &quot;Please provide a value&quot;
      fi
    done
  else
    VAL=${@:3:($#-2)}
  fi
  VAL=$(printf &#039;%s&#039; &quot;$VAL&quot;)
  eval $1=$VAL
  local rstr=$(printf &#039;%q&#039; &quot;$VAL&quot;)
  rstr=$(echo $rstr | sed -e &#039;s/[\/&amp;]/\\&amp;/g&#039;) # escape search string for sed http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
  sed -i &quot;s/&lt;$1&gt;/$rstr/g&quot; $SERVICE_FILE
}

prompt_token &#039;NAME&#039;        &#039;Service name&#039; $1
if [ -f &quot;/etc/init.d/$NAME&quot; ]; then
  echo &quot;Error: service &#039;$NAME&#039; already exists&quot;
  exit 1
fi

prompt_token &#039;DESCRIPTION&#039; &#039; Description&#039; $2
prompt_token &#039;COMMAND&#039;     &#039;     Command&#039; $3
prompt_token &#039;USERNAME&#039;    &#039;        User&#039; $4
if ! id -u &quot;$USERNAME&quot; &amp;&gt; /dev/null; then
  echo &quot;Error: user &#039;$USERNAME&#039; not found&quot;
  exit 1
fi

echo &quot;&quot;

echo &quot;--- Installation ---&quot;
if [ ! -w /etc/init.d ]; then
  echo &quot;You didn&#039;t give me enough permissions to install service myself.&quot;
  echo &quot;That&#039;s smart, always be really cautious with third-party shell scripts!&quot;
  echo &quot;You should now type those commands as superuser to install and run your service:&quot;
  echo &quot;&quot;
  echo &quot;   mv \&quot;$SERVICE_FILE\&quot; \&quot;/etc/init.d/$NAME\&quot;&quot;
  echo &quot;   touch \&quot;/var/log/$NAME.log\&quot; &amp;&amp; chown \&quot;$USERNAME\&quot; \&quot;/var/log/$NAME.log\&quot;&quot;
  echo &quot;   update-rc.d \&quot;$NAME\&quot; defaults&quot;
  echo &quot;   service \&quot;$NAME\&quot; start&quot;
else
  echo &quot;1. mv \&quot;$SERVICE_FILE\&quot; \&quot;/etc/init.d/$NAME\&quot;&quot;
  mv -v &quot;$SERVICE_FILE&quot; &quot;/etc/init.d/$NAME&quot;
  echo &quot;2. touch \&quot;/var/log/$NAME.log\&quot; &amp;&amp; chown \&quot;$USERNAME\&quot; \&quot;/var/log/$NAME.log\&quot;&quot;
  touch &quot;/var/log/$NAME.log&quot; &amp;&amp; chown &quot;$USERNAME&quot; &quot;/var/log/$NAME.log&quot;
  echo &quot;3. update-rc.d \&quot;$NAME\&quot; defaults&quot;
  update-rc.d &quot;$NAME&quot; defaults
  echo &quot;4. service \&quot;$NAME\&quot; start&quot;
  service &quot;$NAME&quot; start
fi

echo &quot;&quot;
echo &quot;---Uninstall instructions ---&quot;
echo &quot;The service can uninstall itself:&quot;
echo &quot;    service \&quot;$NAME\&quot; uninstall&quot;
echo &quot;It will simply run update-rc.d -f \&quot;$NAME\&quot; remove &amp;&amp; rm -f \&quot;/etc/init.d/$NAME\&quot;&quot;
echo &quot;&quot;
echo &quot;--- Terminated ---&quot;</code></pre></div><p>The content of template is as follows:</p><div class="codebox"><pre class="vscroll"><code>#!/bin/sh
### BEGIN INIT INFO
# Provides:          &lt;NAME&gt;
# 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
# Description:       &lt;DESCRIPTION&gt;
### END INIT INFO

SCRIPT=&quot;&lt;COMMAND&gt;&quot;
RUNAS=&lt;USERNAME&gt;

PIDFILE=/var/run/&lt;NAME&gt;.pid
LOGFILE=/var/log/&lt;NAME&gt;.log

start() {
  if [ -f $PIDFILE ] &amp;&amp; [ -s $PIDFILE ] &amp;&amp; kill -0 $(cat $PIDFILE); then
    echo &#039;Service already running&#039; &gt;&amp;2
    return 1
  fi
  echo &#039;Starting service…&#039; &gt;&amp;2
  local CMD=&quot;$SCRIPT &amp;&gt; \&quot;$LOGFILE\&quot; &amp; echo \$!&quot;
  su -c &quot;$CMD&quot; $RUNAS &gt; &quot;$PIDFILE&quot;
 # Try with this command line instead of above if not workable
 # su -s /bin/sh $RUNAS -c &quot;$CMD&quot; &gt; &quot;$PIDFILE&quot;
 
  sleep 2
  PID=$(cat $PIDFILE)
    if pgrep -u $RUNAS -f $NAME &gt; /dev/null
    then
      echo &quot;$NAME is now running, the PID is $PID&quot;
    else
      echo &#039;&#039;
      echo &quot;Error! Could not start $NAME!&quot;
    fi
}

stop() {
  if [ ! -f &quot;$PIDFILE&quot; ] || ! kill -0 $(cat &quot;$PIDFILE&quot;); then
    echo &#039;Service not running&#039; &gt;&amp;2
    return 1
  fi
  echo &#039;Stopping service…&#039; &gt;&amp;2
  kill -15 $(cat &quot;$PIDFILE&quot;) &amp;&amp; rm -f &quot;$PIDFILE&quot;
  echo &#039;Service stopped&#039; &gt;&amp;2
}

uninstall() {
  echo -n &quot;Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] &quot;
  local SURE
  read SURE
  if [ &quot;$SURE&quot; = &quot;yes&quot; ]; then
    stop
    rm -f &quot;$PIDFILE&quot;
    echo &quot;Notice: log file was not removed: $LOGFILE&quot; &gt;&amp;2
    update-rc.d -f $NAME remove
    rm -fv &quot;$0&quot;
  else
    echo &quot;Abort!&quot;
  fi
}

status() {
    printf &quot;%-50s&quot; &quot;Checking &lt;NAME&gt;...&quot;
    if [ -f $PIDFILE ] &amp;&amp; [ -s $PIDFILE ]; then
        PID=$(cat $PIDFILE)
            if [ -z &quot;$(ps axf | grep ${PID} | grep -v grep)&quot; ]; then
                printf &quot;%s\n&quot; &quot;The process appears to be dead but pidfile still exists&quot;
            else    
                echo &quot;Running, the PID is $PID&quot;
            fi
    else
        printf &quot;%s\n&quot; &quot;Service not running&quot;
    fi
}

case &quot;$1&quot; in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo &quot;Usage: $0 {start|stop|status|restart|uninstall}&quot;
esac</code></pre></div><p>The content of ReadMe is as follows:</p><div class="codebox"><pre><code>**This tool directory is used to use the create tool in the directory to generate the service script in /etc/init.d/**</code></pre></div><p>2. Then give it executable permissions, sudo chmod +x /etc/inittools/create<br />3. In this way, every time I need to create a service, I enter /etc/inittools and execute create. It is semi-automatic. Hope it helps you，Cheers</p>]]></description>
			<author><![CDATA[dummy@example.com (dindusmith)]]></author>
			<pubDate>Sun, 22 Sep 2024 13:42:58 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=52384#p52384</guid>
		</item>
	</channel>
</rss>
