Good day,programmers. I have a problem. Please help.
I am creating a service, which must load automatically when Linux is being loaded. So,I copied the script into the directory /etc/rc.d/init.d or /etc/init.d/. But when I am preforming the command
chkconfig --add listOfProcesses
an error occurs:
service listOfProcesses doesn't support chkconfig
Here is the content of the script. I have found the first version in the Google and have used it as a pattern.
#!/bin/bash
# listOfProcesses Start the process which will show the list of processes
# chkconfig: 345 110 02
# description: This process shows current time and the list of processes
# processname: listOfProcesses
### BEGIN INIT INFO
# Provides:
# Required-Start:
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: shows current time and the list of processes
# Description: This process shows current time and the list of processes
### END INIT INFO
# Source function library.
KIND="listOfProcesses"
start() {
echo -n $"Starting $KIND services: "
daemon /home/myscript
echo
}
stop() {
echo -n $"Shutting down $KIND services: "
killproc /home/myscript
echo
}
restart() {
echo -n $"Restarting $KIND services: "
killproc /home/myscript
daemon /home/myscript
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
exit 0;
The second version was made from the cron script. I found the cron script,copied it, and changed it, so I used it as the pattern.
#!/bin/sh
#
# crond Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
# programs at periodic scheduled times. vixie cron adds a \
# number of features to the basic UNIX cron, including better \
# security and more powerful configuration options.
### BEGIN INIT INFO
# Provides: crond crontab
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 2345
# Default-Stop: 90
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified
# programs at periodic scheduled times. vixie cron adds a
# number of features to the basic UNIX cron, including better
# security and more powerful configuration options.
### END INIT INFO
rights=whoami;
root=root;
[ -f "$rights"=="$root" ] || {
echo "this programme requires root rights";
exit 1;
}
# Source function library.
. /etc/rc.d/init.d/functions
start() {
echo -n $"Starting $KIND services: ";
daemon showListOfProcesses;
}
stop() {
echo -n $"Shutting down $KIND services: ";
killproc showListOfProcesses;
}
restart() {
stop
start
}
reload() {
restart;
}
force_reload() {
# new configuration takes effect after restart
restart
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|force-reload}"
exit 2
esac
exit $?
# Show the list of processes
function showListOfProcesses {
top > /dev/tty2;
}
But the situation hadn't changed. What is the problem? What is wrong in the script?
Look at all the scripts that chkconfig can turn on or off in /etc/rc.d/init.d, you'll notice that the top few comments are very important. See How-To manage services with chkconfig and service
#!/bin/sh
#
# crond Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
# programs at periodic scheduled times. vixie cron adds a \
# number of features to the basic UNIX cron, including better \
# security and more powerful configuration options.
You have a script called listofprocesses but to chkconfig this script looks like crond due to the 3rd line and thus it does not find any script called listofprocesses
You'll also most certainly want to change chkconfig: 2345 90 60. Which says which run levels it should be on (in this case 2, 3, 4 and 5), what it's start order is (90) and what its kill order is (60).
You can check the service is correctly set up with chkconfig --list listofprocesses.
Just add the following line at the top:
# chkconfig: - 99 10
it should do the trick
Here is an excellent map of the elements that need to be in an init script, to implement what chkconfig and the init subsystem is doing, and what each element actually does:
http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html
Looks like the max priority is 99, at least on CentOS 6.5, which is what I'm playing with right now.
I was also facing this issue and it was not able to call stop function during shutdown. found the solution after trying so many suggestions on net.
You need to add "touch /var/lock/subsys/" for start and rm -f /var/lock/subsys/" for stop functions in script. Stop may not work for first reboot as lock may be not available during shutdown but will start working from next reboot.
Enjoy....:)
Satya
Related
I have an embedded console application created with Qt,C++. I use nanopi fire (FriendlyARM) for my device with Linux arch. I connect it's gpio as relay for turning on and off the lamps of my room. Also I wrote mobile app to connect the device with socket. When I run my program from putty ./SmartKeyC it runs and I can switch the lamps with my mobile app buttons and all of them are ok.
But when I put my program on start up for auto run, all of functions do their tasks with 2-3 seconds delay.
I used this link to create auto run app. this is my script:
#! /bin/sh
### BEGIN INIT INFO
# Provides: <your script name>
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage my cool stuff
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/root
. /lib/init/vars.sh
. /lib/lsb/init-functions
. /root/
# If you need to source some other scripts, do it here
case "$1" in
start)
log_begin_msg "Starting my super cool service"
# do something
/root/SmartKeyC
log_end_msg $?
exit 0
;;
stop)
log_begin_msg "Stopping the coolest service ever unfortunately"
# do something to kill the service or cleanup or nothing
log_end_msg $?
exit 0
;;
*)
echo "Usage: /etc/init.d/<your script> {start|stop}"
exit 1
;;
esac
and after update rc I have:
root#NanoPi2-Fire:/etc# find -iname "*SmartKeyScript"
./init.d/SmartKeyScript
./rc0.d/K01SmartKeyScript
./rc1.d/K01SmartKeyScript
./rc2.d/S03SmartKeyScript
./rc3.d/S03SmartKeyScript
./rc4.d/S03SmartKeyScript
./rc5.d/S03SmartKeyScript
./rc6.d/K01SmartKeyScript
Where is the problem?
Why when I run my app in putty , everything works good , but when my app starts from auto run it has delay for each function calling?
I remove my script from rcX and put file in my rc.local like below:
Vi /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/local/bin/gen-friendlyelec-release
. /etc/friendlyelec-release
if [ ! -f /etc/firstuse ]; then
/bin/echo ${BOARD} > /etc/hostname
/bin/sed -i "s/\(127.0.1.1\s*\).*/\1${BOARD}/g" /etc/hosts
/bin/hostname ${BOARD}
/bin/echo "0" > /etc/firstuse
fi
. /usr/bin/setqt5env
/usr/bin/lcd2usb_print "CPU: {{CPU}}" "Mem: {{MEM}}" "IP: {{IP}}" "LoadAvg: {{LOADAVG}}" 2>&1 > /dev/null&
#/opt/QtE-Demo/run.sh&
/root/SmartKeyC & > /dev/tty0
exit 0
I've written and compiled a daemon program in C which is meant to run in the background with root access. My program uses libcurl to make some occasional network calls. I've also written a simple init.d script to govern its startup and shutdown procedures. I would like this service to automatically start on boot, and based on what I've done I would expect it to already be doing this. However, I'm noticing an error in the logs relating to libcurl, and as a result the service is not being started automatically.
My program is located in /usr/bin/myprog and I have the following bash script located in /etc/init.d/myprog:
#!/bin/bash
### BEGIN INIT INFO
# Provides: myprog
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: myprog
# Description: My Daemon Program
### END INIT INFO
. /lib/lsb/init-functions
SCRIPT=/usr/bin/myprog
PIDFILE=/var/run/myprog.pid
start() {
if [ -f $PIDFILE ]; then
echo "Service is already started"
return 2
else
$SCRIPT
$RETVAL="$?"
return "${RETVAL}"
fi
}
stop() {
if [ -f $PIDFILE ]; then
kill $(cat $PIDFILE)
rm -f $PIDFILE
return 0
else
echo "Service is not running"
return 2
fi
}
case "$1" in
start)
log_daemon_msg "Starting myprog" "myprog"
start
;;
stop)
log_daemon_msg "Stopping myprog" "myprog"
stop
;;
status)
status_of_proce "$SCRIPT" "myprog" && exit 0 || exit $?
;;
restart)
log_daemon_msg "Restarting myprog" "myprog"
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}" >&2
exit 3
;;
esac
I then ran sudo update-rc.d myprog defaults and this created the following files:
/etc/rc0.d/K01myprog
/etc/rc1.d/K01myprog
/etc/rc2.d/K01myprog
/etc/rc3.d/S02myprog
/etc/rc4.d/S02myprog
/etc/rc5.d/S02myprog
/etc/rc6.d/K01myprog
And as far as I can tell, each of those 7 files are identical copies of the one I posted above. Based on the various tutorials and forums I've been reading, I would think this would be sufficient. However, my service does not appear to be auto-starting on boot. If I call sudo /etc/init.d/myprog start directly, then it starts up fine. But otherwise it does not appear to be launched.
I then noticed an error message in the logs which said "curl error: could not resolve host," even though the particular host it was referencing was definitely valid. So I think perhaps it's trying to launch my application before something that libcurl needs is ready, and is therefore failing to launch. Again, if I launch it manually it works fine. How can I fix this?
If DNS resolution is the point of failure then use the IP address instead. If you are unwilling to use the IP address, add the hostname and IP address to /etc/hosts.
I recently wrote a simple server in Go:
package main
import (
"net/http"
"fmt"
"os/exec"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":****", nil)
}
func handler(output http.ResponseWriter, input *http.Request) {
instruction := "Instructed to " + input.URL.Path[1:] + "."
fmt.Printf(instruction)
if input.URL.Path[1:] == "********" {
*************
*************
*************
if err != nil {
fmt.Println("There was a problem executing the script.")
}
} else {
fmt.Println(" I'm unfamiliar with this instruction.")
}
}
It works perfectly well if compiled and then executed by ./go_http_server &.
The problem is that it doesn't survive reboots. So after some reading, I attempted to daemonize it by placing a script in /etc/init.d:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Put a short description of the service here
# Description: Put a long description of the service here
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/****/****
DAEMON=$DIR/go_http_server
DAEMON_NAME=*********
# Add any command line options for your daemon here
DAEMON_OPTS=""
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=*****
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
...then running update-rc.d go_http_server defaults, and poof! It runs on boot, as verified by ps -ef | grep go_http_server.
But it doesn't receive GET requests while running as a service. Thinking it might be running before the network interface was up, I tried service go_http_server stop, followed by service go_http_server start; still refused to receive GET requests. Stopping the service again and then executing ./go_http_server & makes the server function correctly once more.
I've been Googling this on and off for a couple days now. Either my search queries suck, or this isn't an obvious problem. How do I daemonize my Go server?
EDIT: The exact same thing happens with a server I wrote in Python: it works as it should when executed using ./python_server.py, but--if started as service--HTTP requests are ignored. Both files have been made executable, and it doesn't matter if the daemon user is root or any other user. Not sure if this helps, but I thought it might be relevant.
Supervisor is a good fit here, and can automatically capture and rotate logs written to stdout, restart on crash and manage ports/permissions.
Here's what an example configuration would look like for a Go web service:
# where 'mygoapp' is the name of your application
$ sudo vim /etc/supervisor/conf.d/mygoapp.conf
[program:yourapp]
command=/home/yourappuser/bin/yourapp # the location of your app
autostart=true
autorestart=true
startretries=10
user=yourappuser # the user your app should run as (i.e. *not* root!)
directory=/srv/www/yourapp.com/ # where your application runs from
environment=APP_SETTINGS="/srv/www/yourapp.com/prod.toml" # environmental variables
redirect_stderr=true
stdout_logfile=/var/log/supervisor/yourapp.log # the name of the log file.
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
I wrote an article[1] that takes you through the steps, but the Supervisor documentation is extremely comprehensive.
Similarly, Debian systems also use systemd[2], which can achieve this as well.
In order to install LibreOffice 4.4 into my Debian 8 (Jessie), I just got all my bash scripts from my Debian 7.5 and run them into the same way into the D8 one.
I know there was several changes into the new version but I'm not able to use my service like this anymore :
sudo service libreoffice start
When doing this doesn't start anything and I have to start it using :
sudo /etc/init.d/libreoffice start
And strange thing, when doing (bad parameter) :
sudo service libreoffice dzedjiodjzedj
...the script is perfectly executed and it displays my catched error
Here is my /etc/init.d/libreoffice file :
#
# libreoffice This shell script takes care of starting and stopping the LibreOffice Daemon
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: libreofficedaemon
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Init.d script to run a LibreOffice Daemon
# Short-Description: start and stop LibreOffice Daemon
### END INIT INFO
NAME="LibreOffice Service"
LIBREOFFICE_HOME=/opt/libreoffice4.4
LIBREOFFICE_USER=libreoffice
export LIBREOFFICE_HOME LIBREOFFICE_USER
start() {
echo -ne "Starting $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/start.sh"
}
stop() {
echo -ne "Stopping $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/stop.sh"
}
kill() {
echo -ne "Force close of $NAME. "
killall -u $LIBREOFFICE_USER
}
cd $LIBREOFFICE_HOME
case "$1" in
start|stop)
$1;;
restart) stop; start;;
kill) kill;;
*)
echo "Usage: /etc/init.d/libreoffice {start|stop|restart|kill}"
exit 1
;;
esac
exit 0
And I just run that issue with tomcat8 service yesterday, I just started manually the service and sudo service tomcat8 start worked after that but nothing for libreoffice one..
From the Debian Jessie Release Notes :
When you are asked if any file in the /etc/init.d directory, or the /etc/manpath.config file should be replaced by the package maintainer's version, it's usually necessary to answer “yes” to ensure system consistency
With systemd you now have to use systemctl:
sudo systemctl start libreoffice
Here's some more info
I'm working on a Raspberry Pi running Raspbian running a Node.js app and trying to get it to start when the Pi boots. I found a couple of examples but I can't seem to get it working. My current code is:
#! /bin/sh
# /etc/init.d/MyApp
### BEGIN INIT INFO
# Provides: MyApp.js
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts MyApp.js
# Description: Start / stop MyApp.js at boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting MyApp.js"
# run application you want to start
node /home/pi/app/MyApp/MyApp.js
;;
stop)
echo "Stopping MyApp.js"
# kill application you want to stop
killall MyApp.js
;;
*)
echo "Usage: /etc/init.d/MyApp {start|stop}"
exit 1
;;
esac
exit 0
I have this in the etc/init.d folder, ran chmod +x /etc/init.d/MyApp, I'm able to run it manually, then I run sudo update-rc.d MyApp defaults, reboot and the script never runs. I've looked at some different examples, made adjustments and still no luck.
I solved this problem by first checking where node.js was installed on RaspberryPi:
which node
This gave me :
/usr/local/bin/node
Open crontab config:
sudo crontab -e
Then in my crontab :
#reboot sudo /usr/local/bin/node <complete path to your .js app> &
Save, reboot, and problem solved !
Mohit is right, but just for clarification, you can use readlink to find the full path for your Node.js app as it will be needed later to add as a cron job.
readlink -f <<name of file >>
For instance readlink -f HAP-NodeJS/Core.js results in /home/pi/HAP-NodeJS/Core.js
You can also use which node to find the full path where node.js is installed
Next, create a new cron job using sudo crontab -e and add the following code at the very end:
#reboot sudo /usr/local/bin/node <<.js application path>> &
for instance, my code looks like this.
#reboot sudo /usr/local/bin/node /home/pi/HAP-NodeJS/Core.js &
Upon reboot (or start up) , your Node.js should run. Hope this clears things.
If you're using a prebuilt Pi release like 0.10.24, you may be experiencing a PATH issue.
You can either provide the full path to the node binary as part of the start command or make sure the PATH to the node binaries are set before /etc/init.d/MyApp is ran. I had the same issue and tried both with success. Also, the stop command as you have it may not be working.
#! /bin/sh
# /etc/init.d/test
### BEGIN INIT INFO
# Provides: test
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting test.js"
# run application you want to start
#node /home/pi/test.js > /home/pi/test.log
/home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
;;
stop)
echo "Stopping test.js"
# kill application you want to stop
killall -9 node
# Not a great approach for running
# multiple node instances
;;
*)
echo "Usage: /etc/init.d/test {start|stop}"
exit 1
;;
esac
exit 0
If you'd like to do sudo node, you can add the PATH to Defaults secure_path using sudo visudo.
Also, I would recommend using something like forever to keep your process running after crashes and what not.