I'm somewhat new to Linux and OpenXCAP and I'm trying to make an init.d script for OpenXCAP on CentOS 6.
My script can start and stop OpenXCAP service, but it returns this error for the status command (service openxcap status): openxcap dead but subsys locked
Maybe somebody can tell me if problem is in the init.d script or the openxcap service itself? Is openxcap missing some 'give-status' feature?
#!/bin/bash
#
# Startup script for OpenXCAP
#
# processname: openxcap
# pidfile: /var/run/openxcap/openxcap.pid
# chkconfig: - 85 15
# description: start, stop, restart OpenXCAP server
#
### BEGIN INIT INFO
# Provides: openxcap
# Required-Start: $local_fs $network
# Should-Start: mysqld
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
APP_NAME=openxcap
APP_HOME=/usr/local/src/openxcap-2.0.1
PID_PATH=/var/run/openxcap/openxcap.pid
RETVAL=0
[ -f /etc/sysconfig/$APP_NAME ] && . /etc/sysconfig/$APP_NAME
start()
{
echo -n $"Starting $APP_NAME: "
daemon $APP_HOME/$APP_NAME $OPTIONS 2>/dev/null | tail -1
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/$APP_NAME
}
stop()
{
echo -n $"Stopping $APP_NAME: "
killproc -p $PID_PATH
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/$APP_NAME $PID_PATH
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $APP_NAME
RETVAL=$?
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $APP_NAME {start|stop|reload|restart|status|help}"
exit 1
esac
exit $RETVAL
You are (hopefully) writing out a PID file as /var/run/openxcap/openxcap.pid.
I suspect that your program is writing out one PID, but then starting another process. The first process dies, so sysvinit doesn't know to look for a different one.
However, the lock file indicating that your process was started is still present.
You may not be able to directly use the daemon function for starting this program; you might need to create a customized version that is “smart enough” to identify the correct PID.
Related
I started a omx instance based on this script. it works great, but I can't quit the player with q or alt f4 or ctrl-z.in desktop the keyboard commands always arrive on the desktop. especially if I start the script on the raspberry in CLI mode, I would never get out of the video.
What can I do here?
#!/bin/bash
### BEGIN INIT INFO
# Provides: omxplayer
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Displays video file in a loop using omxplayer
# Description:
### END INIT INFO
# Video (replace with the path and file name of your video)
video_path=/home/pi/video.mp4
#---- There should be no need to edit anything below this line ----
# Start displaying the video loop
case "$1" in start)
screen -dmS videoloop sh -c "omxplayer -o both $video_path -b --loop --no-osd"
echo "Video Playback Started"
;;
# Stop displaying video loop
stop)
sudo killall omxplayer.bin
echo "Video Playback Stopped"
;;
# Restart video loop if it died
repair)
if !(ps -a | grep omx -q)
then
screen -dmS videoloop sh -c "omxplayer -o local $video_path -b --loop --no-osd"
echo "The Video is now running"
fi
;;
*)
echo "Usage: /etc/init.d/videoloop {start|stop|repair}"
exit 1
;;
esac
On a Linux system I'm running a bash script what is running fine, but now I want to use the same script on an AIX system.
Only a small part of it isn't able to run because on the AIX system the command "date -d" is not working.
Below is the code that is running on the linux server, baised on the hostname in this case system1 and system2 a script will run only on a Sunday at the given time.
declare -A myservertime
myservertime["system1"]="02:00"
myservertime["system2"]="02:10"
for keys in "${!myservertime[#]}";
do
if [[ "$keys" == "$HOSTNAME" ]]; then
mylaunchtime=${myservertime["$keys"]}
fi
done
if [ -z "$mylaunchtime" ]; then
echo "Server not found."
exit
fi
timenow=$(date +"%s")
weekday=$(date +"%a" -d #$timenow )
timerun=$(date +"%s" -d ${mylaunchtime} )
if [ $timerun -lt $timenow ]; then
timerun=$(( timerun + 86400 ))
fi
sleep_time=$(( timerun - timenow ))
#Check is weekday = Sunday
if [ $weekday == "Sun" ]; then
#If Sunday then wait until starttime is reached, until then sleep
sleep $sleep_time
#If starttime is reached execute startprogram
startprogram
fi
I already fixed the declare part, this is also not working on AIX, to the following:
typeset -A myservertime=(["pvm00066"]="02:30" ["pvm00100"]="01:30")
But I have troubles to get the variables $weekday and $timerun working, because of that "date -d $variable"
Anyone here that can help me with this, I tried and searched all kind of stuff but I was unable to get one working.
When you do want to have an identical crontab and script on all systems, and you only want to run the script on Sunday, just use an offset from 00:00.
Crontab:
0 0 * * 7 start_with_delay.sh
start_with_delay.sh:
declare -A myservertime
myserversleep["system1"]=7200
myserversleep["system2"]=7800
sleep ${myserversleep[HOSTNAME]}
startprogram
When you do not need fixed starting times for the different hosts, but only want different hosts to have different starting times, you can consider using the the last number of the IP as an input for calculating the sleep time.
Two reasons why you might consider another solution:
Starting a job with crontab, that starts with sleeping, is very confusing.
An operator reading the crontab expects something to start at the time given in the crontab.
Using a scriptname like sleep_and_start helps a little.
When the system reboots at Sunday 01:00, nothing is started at 02:00
The sleep command has been stopped during the reboot.
I would try to find a smart install script, that will write a different crontab on each host. Something with Ansible, a Puppet template or shellscript.
case $HOSTNAME in
system1)
starthour=2
startminute=0
;;
system2)
starthour=2
startminute=10
;;
*)
echo "No scheduled startprogram on ${HOSTNAME}.
exit 0
;;
esac
echo "${startminute} ${starthour} * * SUN startprogram" | crontab -
When you want to update en existing crontab, you might want something like
(crontab -l 2>/dev/null && echo "${startminute} ${starthour} * * 7 startprogram") |
sort -u | crontab -
I am writing a bash script to monitor my MongoDB status. once it is crash then restart it. the script is as below:
while true
do
ret = $("mongod --config /etc/mongod.conf")
if $ret == 0
then
echo "I am out with code 0."
break
fi
echo "running again"
done
echo "I am out with code $?"
But it seems doesn't work. Return from the system:
running again
./mongo-text: line 3: mongod --config /etc/mongod.conf: No such file or directory
./mongo-text: line 3: ret: command not found
./mongo-text: line 4: ==: command not found
not sure what the problem is. Any help is appreciated.
Your loop can be made much simpler:
while ! mongod --config /etc/mongod.conf; do
echo "running again" >&2
sleep 1
done
if test -n "$VERBOSE"; then echo 'modgod successful'; fi
Note that the if keyword executes a command. So if $ret == 0 attempts to run the command $ret (assuming that variable is non-empty and contains no whitespace) with the arguments == and 0. That is almost certainly not what you intend. It is more typical to write if test "$ret" = 0 or if [ "$ret" = 0 ]. If $ret is empty, then it is attempting to execute the command == with the single argument 0.
There are several issues in your code:
$("mongod --config /etc/mongod.conf") will try to run mongod --config /etc/mongod.conf as a command, with spaces included
the if syntax is wrong
You can rewrite it this way:
while :; do
if mongod --config /etc/mongod.conf; then
echo "I am out with code 0."
break
fi
echo "running again"
# probably sleep for a few seconds here
done
echo "I am out with code $?"
For info about if statements, see:
How to check the exit status using an if statement
How to compare strings in Bash
Compound if statements with multiple expressions in Bash
I'm building an haproxy config file that has multiple front and backends. It's going to be several hundred lines long and I'd rather split it up into separate files for each of the different websites that I want to loadbalance.
Does HAProxy offer the ability to link to partial config files from the main haproxy.cfg file?
Configuration files can't be linked together from a configuration directive.
However HAProxy can load multiple configuration files from its command line, using the -f switch multiple times:
haproxy -f conf/http-defaults -f conf/http-listeners -f conf/tcp-defaults -f conf/tcp-listeners
If you want to be flexible with the amount of config files you can even specify a directory like this: -f /etc/haproxy. The files will then be used in their lexical order, newer files overriding older files.
See the mailing list for an example, if provides links to the documentation. This information can be found in the management guide, not the regular docs.
Stumbled on this answer where the author created scripts to imitate nginx disable enable sites functionality. In the haproxy init.d startup he uses script loop to build the haproxy -f commands concatenation.
/etc/init.d/haproxy:
EXTRAOPTS=`for FILE in \`find /etc/haproxy/sites-enabled -type l | sort
-n\`; do CONFIGS="$CONFIGS -f $FILE"; done; echo $CONFIGS`
haensite script:
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "You must be a root user" 2>&1
exit 1
fi
if [ $# -lt 1 ]; then
echo "Invalid number of arguments"
exit 1
fi
echo "Enabling $1..."
cd /etc/haproxy/sites-enabled
ln -s ../sites-available/$1 ./
echo "To activate the new configuration, you need to run:"
echo " /etc/init.d/haproxy restart"
hadissite script:
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "You must be a root user" 2>&1
exit 1
fi
if [ $# -lt 1 ]; then
echo "Invalid number of arguments"
exit 1
fi
echo "Disabling $1..."
rm -f /etc/haproxy/sites-enabled/$1
echo "To activate the new configuration, you need to run:"
echo " /etc/init.d/haproxy restart"
This was a solution building off of #stephenmurdoch's answer which involved the use of multiple -f <conf file> arguments to the haproxy executable.
Using the stock CentOS 6.x RPM's included /etc/init.d/haproxy script you can amend it like so:
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
#daemon $exec -D -f $cfgfile -f /etc/haproxy/haproxy_ds.cfg -f /etc/haproxy/haproxy_es.cfg -f /etc/haproxy/haproxy_stats.cfg -p $pidfile $OPTIONS
daemon $exec -D -f $cfgfile $(for i in /etc/haproxy/haproxy_*.cfg;do echo -n "-f $i ";done) -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
With the above in place you can then create files such as haproxy_<X>.cfg and haproxy_<Y>.cfg using whatever names you want. The above for loop will include these files in an augmented daemon haproxy ... line if these files are present, otherwise the stock haproxy.cfg file will be used solely.
Within the haproxy_<...>.cfg files you need to make sure that your global and defaults are defined in the "toplevel" haproxy.cfg file. The rest of the files simply need to have frontend/backends and nothing more.
You can follow this simple step.
Insert one line script (cat /etc/$BASENAME/conf.d/*.cfg > $CFG) in /etc/init.d/haproxy
Here is position where you must insert line
CFG=/etc/$BASENAME/$BASENAME.cfg
cat /etc/$BASENAME/conf.d/*.cfg > $CFG
[ -f $CFG ] || exit 1
Reload daemon config with systemctl daemon-reload
Make directory mkdir /etc/haproxy/conf.d
Move default haproxy.cfg to conf.d as global.cfg mv /etc/haproxy/haproxy.cfg /etc/haproxy/conf.d/global.cfg
Create your other .cfg file in conf.d directory
Just restart your haproxy service systemctl restart haproxy
NOTE: /etc/haproxy/haproxy.cfg will be automaticly created from all files in conf.d/
answer of #Bapstie memtioned that, a directory can be passed to haproxy as config file, and files inside will be loaded in alphabet order. It's correct.
But problem is, the package haproxy in CentOS 'base/7/x86_64' repository is so old that it does not support that.
So either do you need to write a wrapper to append -f <individual config file>to the command, or you need to install latest version of haproxy:
for package in centos-release-scl-rh rh-haproxy18-haproxy; do
yum install -y $package
done
and create a drop-in config for haproxy service:
[Service]
ExecStart=
ExecStart=/opt/rh/rh-haproxy18/root/sbin/haproxy -f /etc/haproxy-nutstore/ -p /run/haproxy.pid $OPTIONS
If you use Ansible you can do a trick like this:
- name: haproxy configuration
copy:
content: >
{{ lookup('template', haproxy_cfg_src_top) +
lookup('template', haproxy_cfg_src_edge) +
lookup('template', haproxy_cfg_src_bottom) }}
dest: "{{ haproxy_cfg }}"
owner: "{{ docker_user }}"
group: "docker"
mode: 0664
register: haproxy_cfg_change
I'm on a headless RaspberryPi (Raspbian) and I would like this service to autostart when system starts up:
#!/bin/bash
### BEGIN INIT INFO
# Provides: mnt
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mount/unmount volumes from /etc/fstab
### END INIT INFO
#VARIABLES for the truecrypt volume
PROTECT_HIDDEN=no
KEYFILES=""
PASSWORD_FILE=/etc/truecrypt
mount_all(){
slot=0
while read line;
do
read -a fields <<< $line
VOLUME_PATH=${fields[0]}
MOUNT_DIRECTORY=${fields[1]}
FILESYSTEM=${fields[2]}
OPTIONS=${fields[3]}
slot=$((slot+1))
truecrypt \
--text \
--verbose \
--keyfiles=$KEYFILES \
--protect-hidden=$PROTECT_HIDDEN \
--slot=${slot} \
--fs-options=$OPTIONS \
--filesystem=$FILESYSTEM $VOLUME_PATH $MOUNT_DIRECTORY \
< <(grep $VOLUME_PATH $PASSWORD_FILE | sed "s,^${VOLUME_PATH}:,,") \
| grep -v "Enter password for"
done < <(grep '^##truecrypt' /etc/fstab | sed 's/##truecrypt://g')
}
# Function to redirect the output to syslog
log_to_syslog(){
# Temporal file for a named pipe
script_name=$(basename "$0")
named_pipe=$(mktemp -u --suffix=${script_name}.$$)
# On exit clean up
trap "rm -f ${named_pipe}" EXIT
# create the named pipe
mknod ${named_pipe} p
# start syslog and redirect the named pipe
# append the script name before the messages
logger <${named_pipe} -t $0 &
# Redirect stout and stderr to the named pipe
exec 1>${named_pipe} 2>&1
}
# If the script does not run on a terminal then use syslog
set_log_output(){
if [ ! -t 1 ]; then
log_to_syslog
fi
}
case "$1" in
''|start)
EXITSTATUS=0
set_log_output
mount_all || EXITSTATUS=1
exit $EXITSTATUS
;;
stop)
EXITSTATUS=0
set_log_output
truecrypt --verbose --force --dismount || EXITSTATUS=1
exit $EXITSTATUS
;;
restart|force-reload)
EXITSTATUS=0
$0 stop || EXITSTATUS=1
$0 start || EXITSTATUS=1
exit $EXITSTATUS
;;
status)
EXITSTATUS=0
truecrypt --list 2>/dev/null || echo "No truecrypt volumes mounted"
exit $EXITSTATUS
;;
*)
echo "Usage: $0 [start|stop|restart]"
exit 3
;;
esac
The service has 755 permisisons and is owned by root. After setting the permission I did (with no errors):
update-rc.d mnt defaults
When I start the service manually immediately after startup it works well.
Where may be the problem? It would be also great to use this service as a required prerequisite for autostarting Samba - is it possible?
The solution was pretty simple. I installed truecrypt only as a binary and I had environment variable path to truecrypt set only for user, not root or any other system user which is used for autostart.
The solution was to change truecrypt command to /path_to_truecrypt/truecrypt.