How to create a service on linux Centos 6 - linux

How to create service on centos 6 to kill service and run script
For example :
If i run this command service test start
it should make the below
pkill test
nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml
Also if i run this command service test stop
it should make the below
pkill test

Create a file like /etc/init.d/test
#!/bin/bash
start() {
/usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml 2>&1 &
echo $! > /tmp/test_nohup_pid.txt
}
stop() {
/usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

Related

Script guide to prevent duplicate execution of jar in Linux

Jar should run in the background and not run redundantly.
Even if duplicate execution occurs, all must be terminated
#!/bin/bash
ENV=dev
SER_NAME=batch
JAR_FULL=/was/batch/test.jar
case $1 in
restart)
if apid=$(pgrep -f $SER_NAME)
then
for pid in $apid; do
echo "Stop $SER_NAME pid : $pid"
kill -9 $pid
done
else
echo "$SER_NAME is not running ... "
fi
nohup java -jar $JAR_FULL --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo "$SER_NAME" start"
;;
stop)
if apid=$(pgrep -f $SER_NAME)
then
for pid in $apid; do
echo "Stop $SER_NAME - pid : $pid"
kill -9 $pid
done
else
echo "$SER_NAME is not running ..."
fi
;;
start)
if apid=$(pgrep -f $SER_NAME)
then
echo "$SER_NAME is already running ..."
for pid in $apid; do
echo "Running $SER_NAME -pid : $pid"
done
else
nohup java -jar $JAR_FULL --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo "$SER_NAME start!"
fi
;;
esac
example) run.sh
console) chmod 755 run.sh
console) ./run.sh start
start or stop or restart

init.d scripts works perfectly in console, bad poorly in systemd's service call

i have following init.d script.
#!/bin/sh
### BEGIN INIT INFO
# Provides: teamcity
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
start_cmd="start-stop-daemon --start -c root --chdir /srv/teamcity/TeamCity-9.1/bin --exec /srv/teamcity/TeamCity-9.1/bin/runAll.sh start"
stop_cmd="start-stop-daemon --start -c root --chdir /srv/teamcity/TeamCity-9.1/bin --exec /srv/teamcity/TeamCity-9.1/bin/runAll.sh stop"
user="root"
export TEAMCITY_DATA_PATH="/srv/teamcity/TeamCity-9.1/.BuildServer"
export TEAMCITY_PID_FILE_PATH="/var/run/teamcity.pid"
export TEAMCITY_SERVER_OPTS=-Djava.awt.headless=true
export TEAMCITY_SERVER_MEM_OPTS="-Xmx750m -XX:MaxPermSize=270m"
/etc/profile.d/java.sh
name="teamcity"
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
$start_cmd >> "$stdout_log" 2>> "$stderr_log" &
sleep 1
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
$stop_cmd >> "$stdout_log" 2>> "$stderr_log" &
for i in {1..20}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
If you using it directly from console like this
/etc/init.d/teamcity start
,it works very well. But with system'd "service" command not works.
Systemd is not be able to start Teamcity properly.
Systemd writes only one line on service teamcity start:
Started LSB: Start daemon at boot time.
Stopping service fails with:
teamcity.service: control process exited, code=exited status=1
Stopped LSB: Start daemon at boot time.
Unit teamcity.service entered failed state.
I spend three days googling and changing init file, but no suitable solution.
Any suggestions to fix that?
Leave SYSV init.d scripts and start using new systemd services notation is my last solution.
Thanks a lot
Here's a simple systemd service file that I stitched together that works for my installed version of TeamCity: v10.0.4 (build 42538):
[Unit]
Description=TeamCity Build Agent
After=network.target
[Service]
Type=simple
Environment="JAVA_HOME=/usr/java/jdk1.8.0_121"
ExecStart=/opt/teamcity/bin/startup.sh
ExecStop=/opt/teamcity/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
You'll need to customize the Environment, ExecStart, and ExecStop variables to suit your installation of Java and TeamCity.

How to kill all processes named "shairport" that do not have the pid 12345

I am using shairport at work to stream music. I am running it on a Debian machine(a raspberry). In its /etc/init.d/shairport file it has only the start|stop commands.
I want to add a restart one. Here is the code so far:
case "$1" in
restart)
service shairport stop
service shairport start
;;
start)
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
stop)
killall shairport
;;
*)
echo "Usage: /etc/init.d/shairport {start|stop|restart}"
exit 1
;;
esac
exit 0
The issue is that when I run "service shairport restart", the service is stopped, thus running "killall shairport" and killing the bash script process itself. So "start" is never executed.
How do I make killall kill every shairport except the current script?
My idea was to get the pid and exclude it, but I cant find how to do that.
The start part should write down the PID of the started process.
Something like echo $? > /var/run/shairport.pid will do.
Stop part of your script will use the PID from the .pid file we have created, and kill the right process. This is what most of Linux services do as far as I know.
In linux you can know the process ID that the script file is running under with : $$.
You just have to check you're not killing yourself with :
stop)
for pid in $(pgrep shairport); do
if[$pid != $$]
kill $pid
fi
done
Since the answer I chose did not work straight out of the box, here is the code I ended up with:
case "$1" in
restart)
for pid in $(pgrep shairport); do
if [ "$pid" != $$ ]; then
kill $pid
fi
done
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
start)
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
stop)
killall shairport
;;
*)
echo "Usage: /etc/init.d/shairport {start|stop|restart}"
exit 1
;;
esac

Run jar file as Daemon on Linux Ubuntu

I want to install a bot to my Teamspeak3 and run this bot as a daemon on startup. I wrote my own script and copied it to init.d and then added it with update-rc.d to defaults.
#!/bin/sh
#
# JTS3ServerBot Script
#
USER="ts"
NAME="jts3"
DIR="/home/ts/jts3/"
case $1 in
start)
echo "Starting ${NAME} ..."
if [ ! -f $DIR/pid ]; then
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is already running ..."
fi
;;
stop)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ..."
kill $PID;
echo "${NAME} stopped ..."
rm $DIR/pid
else
echo "${NAME} is not running ..."
fi
;;
restart)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ...";
kill $PID;
echo "${NAME} stopped ...";
rm $DIR/pid
echo "Starting ${NAME} ..."
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is not running ..."
fi
;;
esac
A pid file in generated, but if i try to kill the process with this pid i get an error that the process does not exist. If i use top there is no process with the pid listed.
root#vps-1023645-8462:~# service jts3 start
Starting jts3 ...
jts3 started ...
root#vps-1023645-8462:~# cat /home/ts/jts3/pid
10206
root#vps-1023645-8462:~# kill 10206
bash: kill: (10206) - No such process
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1762 ts 20 0 1881m 14m 3408 S 0 1.4 215:47.28 ts3server_linux
32356 ts 20 0 164m 1576 1336 S 0 0.2 0:09.85 tsdnsserver_lin
I have found another solution for my problem. I use upstart (works only with Ubuntu) to run my jar-File as a daemon. Upstart manages the PIDs. Just add myservice.conf to /etc/init (not /etc/inid.d) and the daemon will be started on boot and you can mangage it as a service. You do not have to make the file runnable or anything else
You can manage the service as normal for example
service myservice restart
service myservice status
...
My Config-File:
description "myservice"
author "your name"
start on runlevel [3]
stop on shutdown
expect fork
script
cd /home/username/
sudo -u username java -jar /home/username/myservice/myservice.jar >/home/username/myservice.log 2>&1
emit myservice_running
end script
This solution is really easy and works well on my Ubuntu 12.04 Server.
You have an error in this line:
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>>/dev/null >>/dev/null&
You appear to be mixing the syntaxes of sudo and su. Before version 1.8, sudo had no -c option - you just give it the command to run after any other options. In 1.8 there is a -c option but it's not for specifying the command (it's for limiting resource usage to that of a given login class). sudo is printing an error message about this invalid syntax, but you're not seeing it because you're redirecting all the output to /dev/null.
Simply remove the -c to form a valid command:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
Also, you can simplify the command a little by using the 2>&1 syntax to send stderr to the same handle as stdout, and there is no need for append mode when writing to /dev/null:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR >/dev/null 2>&1 &

Call to daemon in a /etc/init.d script is blocking, not running in background

I have a Perl script that I want to daemonize. Basically this perl script will read a directory every 30 seconds, read the files that it finds and then process the data. To keep it simple here consider the following Perl script (called synpipe_server, there is a symbolic link of this script in /usr/sbin/) :
#!/usr/bin/perl
use strict;
use warnings;
my $continue = 1;
$SIG{'TERM'} = sub { $continue = 0; print "Caught TERM signal\n"; };
$SIG{'INT'} = sub { $continue = 0; print "Caught INT signal\n"; };
my $i = 0;
while ($continue) {
#do stuff
print "Hello, I am running " . ++$i . "\n";
sleep 3;
}
So this script basically prints something every 3 seconds.
Then, as I want to daemonize this script, I've also put this bash script (also called synpipe_server) in /etc/init.d/ :
#!/bin/bash
# synpipe_server : This starts and stops synpipe_server
#
# chkconfig: 12345 12 88
# description: Monitors all production pipelines
# processname: synpipe_server
# pidfile: /var/run/synpipe_server.pid
# Source function library.
. /etc/rc.d/init.d/functions
pname="synpipe_server"
exe="/usr/sbin/synpipe_server"
pidfile="/var/run/${pname}.pid"
lockfile="/var/lock/subsys/${pname}"
[ -x $exe ] || exit 0
RETVAL=0
start() {
echo -n "Starting $pname : "
daemon ${exe}
RETVAL=$?
PID=$!
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
echo $PID > ${pidfile}
}
stop() {
echo -n "Shutting down $pname : "
killproc ${exe}
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f ${lockfile}
rm -f ${pidfile}
fi
}
restart() {
echo -n "Restarting $pname : "
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status ${pname}
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;; esac
exit 0
So, (if I have well understood the doc for daemon) the Perl script should run in the background and the output should be redirected to /dev/null if I execute :
service synpipe_server start
But here is what I get instead :
[root#master init.d]# service synpipe_server start
Starting synpipe_server : Hello, I am running 1
Hello, I am running 2
Hello, I am running 3
Hello, I am running 4
Caught INT signal
[ OK ]
[root#master init.d]#
So it starts the Perl script but runs it without detaching it from the current terminal session, and I can see the output printed in my console ... which is not really what I was expecting. Moreover, the PID file is empty (or with a line feed only, no pid returned by daemon).
Does anyone have any idea of what I am doing wrong ?
EDIT : maybe I should say that I am on a Red Hat machine.
Scientific Linux SL release 5.4 (Boron)
Thanks,
Tony
I finally re-wrote the start function in the bash init script, and I am not using daemon anymore.
start() {
echo -n "Starting $pname : "
#daemon ${exe} # Not working ...
if [ -s ${pidfile} ]; then
RETVAL=1
echo -n "Already running !" && warning
echo
else
nohup ${exe} >/dev/null 2>&1 &
RETVAL=$?
PID=$!
[ $RETVAL -eq 0 ] && touch ${lockfile} && success || failure
echo
echo $PID > ${pidfile}
fi
}
I check that the pid file is not existing already (if so, just write a warning). If not, I use
nohup ${exe} >/dev/null 2>&1 &
to start the script.
I don't know if it is safe this way (?) but it works.
The proper way to daemonize a process is have it detach from the terminal by itself. This is how most larger software suites do it, for instance, apache.
The rationale behind daemon not doing what you would expect from its name, and how to make a unix process detach into the background, can be found here in section 1.7 How do I get my program to act like a daemon?
Simply invoking a program in the background isn't really adequate for
these long-running programs; that does not correctly detach the
process from the terminal session that started it. Also, the
conventional way of starting daemons is simply to issue the command
manually or from an rc script; the daemon is expected to put itself
into the background.
For further reading on this topic: What's the difference between nohup and a daemon?
According to man daemon correct syntax is
daemon [options] -- [command] [command args]
Your init script startup should run something like:
daemon --pidfile ${pidfile} -- ${exe}
As said here, it seems that the process needs to be sent to the background using &.
Daemon don’t do it for you.

Resources