【树莓派】在树莓派上制作开机自启动程序及服务2

时间:2021-10-09 15:41:39

有朋友说根据《【树莓派】在树莓派上制作开机自启动程序及服务》这个文章实践时候有问题,可能之前中间有过个更新,但是忘了更新博文,在这里我把目前最新的贴一下。

先简要介绍一下,我这边有2个服务,一个sendtooss,一个scptolocal;

 

/etc/init.d/sendtooss:

sendtooss服务:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          sendtooss
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start: $local_fs
# Required-Stop:
# chkconfig: 12345 99 05
# Short-Description: Start or stop the sendtooss App.
### END INIT INFO

start(){
    echo -e "\033[32m start sendtooss \033[0m"
    cd /home/lifeccp/webapp/sendtooss
    su lifeccp -c "sh /home/lifeccp/webapp/sendtooss/startup --spring.profiles.active=test &"
}

stop(){
    echo -e "\033[32m stop sendtooss \033[0m"
    pkill -f sendtooss

    app_pid=$(pgrep -f sendtooss)
    if [ "${app_pid}" = "" ]; then
        echo -e  "\033[32m -=stop sendtooss finished=- \033[0m"
    else
        echo -e  "\033[31m -=stop sendtooss failed=- \033[0m"
        kill -9 "${app_pid}"
        echo -e  "\033[32m -=kill -9 sendtooss=- \033[0m"
    fi
}

case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
*)
    echo -e "\033[32m Usage: $0 (start|stop) \033[0m"
    ;;
esac

 

/etc/init.d/scptolocal:

scptolocal服务:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          scptolocal
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start: $local_fs
# Required-Stop:
# chkconfig: 12345 99 05
# Short-Description: Start or stop the scptolocal App.
### END INIT INFO

start(){
    echo -e "\033[32m start scptolocal \033[0m"
    cd /home/lifeccp/webapp/scptolocal
    su lifeccp -c "sh /home/lifeccp/webapp/scptolocal/scptolocal -b LIFECCP:5090 --directory /home/lifeccp/dicom &"
}

stop(){
    echo -e "\033[32m stop scptolocal \033[0m"
    pkill -f scptolocal

    app_pid=$(pgrep -f scptolocal)
    if [ "${app_pid}" = "" ]; then
        echo -e  "\033[32m -=stop scptolocal finished=- \033[0m"
    else
        echo -e  "\033[31m -=stop scptolocal failed=- \033[0m"
        kill -9 "${app_pid}"
        echo -e  "\033[32m -=kill -9 scptolocal=- \033[0m"
    fi
}

case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
*)
    echo -e "\033[32m Usage: $0 (start|stop) \033[0m"
    ;;
esac

 

--------------------

在/home/lifeccp/webapp/目录下,需要有相应的shell脚本,被调用。如下:

/home/lifeccp/webapp/sendtooss/startup 脚本内容:

lifeccp@raspberrypi:~ $ cat webapp/sendtooss/startup 
#!/bin/bash
# -------------------------------------------------------------------------
# Osprey  Launcher
# args:
#     --spring.profiles.active = <profile>    e.g.:'dev[test|prod]'
# -------------------------------------------------------------------------

MAIN_JAR=osprey-1.0.14.release.jar

DIRNAME="$(dirname "$0")"

# OS specific support (must be 'true' or 'false').
cygwin=false;
case "$(uname)" in
    CYGWIN*)
        cygwin=true
        ;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
    [ -n "$WORK_HOME" ] &&
        WORK_HOME=$(cygpath --unix "$WORK_HOME")
    [ -n "$JAVA_HOME" ] &&
        JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
fi

# Setup WORK_HOME
if [ "x$WORK_HOME" = "x" ]; then
    WORK_HOME=$(cd "$DIRNAME"; pwd)
fi

# Setup the JVM
JAVA="java"
# if [ "x$JAVA_HOME" != "x" ]; then
#     JAVA=$JAVA_HOME/bin/java
# else
#     JAVA="java"
# fi

# Execute the JVM
cd "$WORK_HOME" && exec "$JAVA" -jar "$MAIN_JAR" "$@"

 

/home/lifeccp/webapp/scptolocal/scptolocal 中有对应的 scptolocal 服务:

lifeccp@raspberrypi:~ $ cat webapp/scptolocal/scptolocal
#!/bin/sh
# -------------------------------------------------------------------------
# Pandion  Launcher
# args:
#     -b,--bind <[aet[@ip]:]port> e.g.:'LIFECCP:107'
#     --directory <path>          e.g.:'./dicom'
# -------------------------------------------------------------------------

MAIN_CLASS=com.lifeccp.box.store.DicomReceiver
MAIN_JAR=pandion-1.0.5.release.jar

DIRNAME="$(dirname "$0")"

# OS specific support (must be 'true' or 'false').
cygwin=false;
case "$(uname)" in
    CYGWIN*)
        cygwin=true
        ;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
    [ -n "$WORK_HOME" ] &&
        WORK_HOME=$(cygpath --unix "$WORK_HOME")
    [ -n "$JAVA_HOME" ] &&
        JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
fi

# Setup WORK_HOME
if [ "x$WORK_HOME" = "x" ]; then
    WORK_HOME=$(cd "$DIRNAME"; pwd)
fi

# Setup the JVM
JAVA="java"
# if [ "x$JAVA_HOME" != "x" ]; then
#     JAVA=$JAVA_HOME/bin/java
# else
#     JAVA="java"
# fi

# Setup the classpath
CP="$WORK_HOME/$MAIN_JAR"

# Execute the JVM
exec "$JAVA" -cp "$CP" $MAIN_CLASS "$@"

 

主要脚本如上,目前我的环境运行正常,如果朋友们在使用过程中有问题,可以留言或通过介绍中的QQ或扫头像加微信联系我。