I have a server running Gentoo 2.6.12 r6 and I want to run Apache Archiva as a standalone server at startup. Does anyone have a working init.d script to accomplish this? Thanks!
Assuming that you have created a user account called archiva and Archiva is installed at /opt/archiva-1.0.
While logged as root, create a the script /etc/rc.d/init.d/archiva as follows:
\#! /bin/sh
start() {
echo "Starting Archiva..."
su -l archiva -c '/opt/archiva-1.0/bin/archiva start > /dev/null 2> /dev/null &'
}
stop() {
echo "Stopping Archiva..."
su -l archiva -c '/opt/archiva-1.0/bin/archiva stop &'
}
restart() {
stop
sleep 60
su -l archiva -c 'killall java'
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: archiva {start|stop|restart}"
exit 1
esac
exit 0
Now execute the following commands as root where SXX and KXX specify the startup and shutdown order. For example S63 and K37
$ chmod 775 /etc/rc.d/init.d/archiva
$ ln -s /etc/rc.d/init.d/archiva /etc/rc3.d/SXXarchiva
$ ln -s /etc/rc.d/init.d/archiva /etc/rc3.d/KXXarchiva
Related
In CentOS 6.8 I have a golang app , that run in command go run main.go and I need to create a system service to run it in boot like service httpd.
I know that I have to create file like /etc/rc.d/init.d/httpd But I don't know how to do it to run that command.
First, you will need to build your Go binary and put it in your path.
go install main.go
If your "main" file is called main, go install will place a binary called "main" in your path, so I suggest you rename your file to whatever you call your project/server.
mv main.go coolserver.go
go install coolserver.go
You can run coolserver to make sure everything is fine. It will if you have your $GOPATH setup properly.
Here it is an example of a init.d service called service.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides: <NAME>
# 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: <DESCRIPTION>
### END INIT INFO
SCRIPT=<COMMAND>
FLAGS="--auth=user:password"
RUNAS=<USERNAME>
PIDFILE=/var/run/<NAME>.pid
LOGFILE=/var/log/<NAME>.log
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting serviceā¦' >&2
local CMD="$SCRIPT $FLAGS &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping serviceā¦' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f <NAME> remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|uninstall}"
esac
Copy to /etc/init.d:
cp "service.sh" "/etc/init.d/coolserver"
chmod +x /etc/init.d/coolserver
Remember to replace
<NAME> = coolserver
<DESCRIPTION> = Describe your service here (be concise)
<COMMAND> = /path/to/coolserver
<USER> = Login of the system user the script should be run as
Start and test your service and install the service to be run at boot-time:
service coolserver start
service coolserver stop
update-rc.d coolserver defaults
I assume you tried to use apache web server. Actually, Go web server is enough itself. Main purpose is to run Go web server in system service.So, you can use tmux https://tmux.github.io/ or nohup to run as system service. You can also use apache or nginx web server as proxy.
So I wrote the Arch Linux rc.d script for mongod daemon (following an example), but when I do:
sudo rc.d start mongod
it just gets stuck on:
:: Starting /usr/bin/mongod [BUSY]
and never transitions to "DONE" phase. Any tips?
Here is my script:
#!/bin/bash
# import predefined functions
. /etc/rc.conf
. /etc/rc.d/functions
# Point to the binary
DAEMON=/usr/bin/mongod
# Get the ARGS from the conf
. /etc/conf.d/crond
# Function to get the process id
PID=$(get_pid $DAEMON)
case "$1" in
start)
stat_busy "Starting $DAEMON"
# Check the PID exists - and if it does (returns 0) - do no run
[ -z "$PID" ] && $DAEMON $ARGS &> /dev/null
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
kill -HUP $PID &>/dev/null
rm_daemon $DAEMON
stat_done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
I've looked at how apache does it, but I can't figure out what they are doing that's different. Here's a piece of their httpd script:
case "$1" in
start)
stat_busy "Starting Apache Web Server"
[ ! -d /var/run/httpd ] && install -d /var/run/httpd
if $APACHECTL start >/dev/null ; then
add_daemon $daemon_name
stat_done
else
stat_fail
exit 1
fi
;;
For one thing, you are passing an $ARGS variable that is never actually defined. You will probably want to either pass some configuration options, or the location of a mongodb.conf file using the -f or --config option, to inform the daemon of the location of your database, log file, IP bindings, etc.
The mongod defaults assume that you database location is /data/db/. If this does not exist, or the daemon does not have permissions to that location, then the init script will fail.
You should probably also run the daemon with a user account other than yourself or root (the default pacman package creates a user named mongodb), and give this user read/write access to the data path and log file.
[ -z "$PID" ] && /bin/su mongodb -c "/usr/bin/mongod --config /etc/mongodb.conf --fork" > /dev/null
I would suggest referring to the mongodb init script provided in the Arch Community package, and comparing that to what you have here. Or, install MongoDB using pacman, which sets all of this up for you.
If all else fails, add some 'echo' commands inside of your if and else blocks to track down exactly where the init script is hanging, check mongodb's logs, and report back to us.
I'm trying to modify this very simple shell script I wrote to check if the screens are already active and if so, not create them. For example, if I call this script twice with the start parameter, four screen sessions will be made. I want to prevent that.
#! /bin/sh
# /etc/init.d/css-server
#
case "$1" in
start)
echo "Starting Nullus Imprimis war server..."
screen -A -m -d -S css-war-server /home/css-servers/war-server/css/srcds_run -game cstrike +map de_dust2 +maxplayers 16 -autoupdate -port 2555
echo "Nullus Imprimis war server started"
echo "Starting Nullus Imprimis pub server #1..."
screen -A -m -d -S css-pub-server-1 /home/css-servers/pub-server-1/css/srcds_run -game cstrike +map de_dust2 +maxplayers 32 -autoupdate -port 2666
echo "Nullus Imprimis pub server #1 started"
;;
stop)
echo "Stopping Nullus Imprimis war server..."
screen -S css-war-server -X quit
echo "Nullus Imprimis war server stopped"
echo "Stopping Nullus Imprimis pub server #1..."
screen -S css-pub-server-1 -X quit
echo "Nullus Imprimis pub server #1 stopped"
;;
*)
echo "Usage: service css-servers {start|stop}"
exit 1
;;
esac
exit 0
Also, I want to make the servers run under their own username, in this case css-servers. How can I do that?
To check if a screen is already running, I usually just grep it from screen -ls:
screen -ls | grep -q NAME || ...do something if server is not running...
Or:
if ! screen -ls | grep -q NAME; then
...do something if server is not running...
fi
In order to run this as a different user, I would suggest running the startup script with sudo -u, something like this:
sudo -u css-servers STARTUP_SCRIPT
I am currently running a Minecraft server in a screen session with this command:
(tail -f /path/to/fifo & cat) | java -Xmx2048M -jar minecraft_server.jar nogui
You can shutdown a minecraft server by sending 'stop' in the server console. I am using the fifo to send commands from other bash scripts, and cat to allow input from the actual Minecraft server console in the screen session.
What happens though, is that if you put the command 'stop' in the actual minecraft console, the server ends up hanging right before it should exit because of the 'cat' command. The only way to get past this, is to press enter again after sending the stop command.
How can I get 'cat' to not cause this to hang?
Edit: The full script.
#!/bin/bash
serverDirectory=/opt/games/minecraft
pidFile=$serverDirectory/server.pid
fifoFile=$serverDirectory/server.fifo
cleanup() {
rm -f $pidFile
rm -f $fifoFile
}
if [ ! -p $fifoFile ]; then
mkfifo $fifoFile && chmod 0777 $fifoFile
fi
echo $$ > $pidFile
# restart server if it stops
while true
do
# how minecraft server should handle an interruption
trap "{ echo 'stop' > $fifoFile ; }" SIGINT
(tail -f $fifoFile & cat) | java -Xmx2048M -jar minecraft_server.jar nogui
echo "Restarting server...."
# if interruption occurs before we restart, stop trying to restart and clean up
trap "{ cleanup ; exit 0 ; }" SIGINT SIGTERM
sleep 5
done
I haven't used a minecraft server, so I don't know if I'm on the right track here, but would this work?
#!/bin/sh
fifo="/path/to/fifo"
mkfifo $fifo
trap "rm -f $fifo" 0 1 2 3 6 15
/path/to/java -Xmx2048M -jar minecraft_server.jar nogui < $fifo &
echo $? > /path/to/minecraft.pid
cat > $fifo
This still doesn't kill off the cat once the server quits, but at least it doesn't block the server. You might want to launch the minecraft server in a function that kills the cat when it exits. I suggest keeping the .pid file for possible future use. :-)
Well.
Hi everybody again.
I have a trouble with some script that I wanna to be executed as a Service in an Ubuntu Server 10.04 LTS PC. This is the script:
#! /bin/sh
JBOSS_BIN=/usr/local/jboss/bin
JBOSS_START_SCRIPT=$JBOSS_BIN/run.sh
JBOSS_STOP_SCRIPT=/usr/local/jboss/bin/shutdown.sh
JBOSS_BIND_ADDR=${JBOSS_HOST:-"-b 0.0.0.0"}
ECHO=/bin/echo
TEST=/usr/bin/test
$TEST -x $JBOSS_START_SCRIPT || exit 0
$TEST -x $JBOSS_STOP_SCRIPT || exit 0
start(){
$ECHO "Starting JBoss"
su - jboss -c $JBOSS_START_SCRIPT $JBOSS_BIND_ADDR "> /dev/null &"
$ECHO "."
}
stop(){
$ECHO "Stopping JBoss"
su - jboss -c $JBOSS_STOP_SCRIPT -S
$ECHO "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 30
start
;;
*)
$ECHO "Usage: jboss (start|stop|restart)"
exit 1
;;
esac
exit 0
Well That script is not working because I don't know exactly how to put "> /dev/null &" for being executed correctly. See, if I do the command in a hand in the gnome-terminal it works, but when I write it in the script and execute it, it fails. So I don't know what is working wrong. Perhaps some buggy syntax? plz help me; I'm really stuck with this.
Leave off the quotes that you have around > /dev/null &
su - jboss -c $JBOSS_START_SCRIPT $JBOSS_BIND_ADDR > /dev/null &