Arquivo da categoria: Shell Script

How to make a jar file run on startup & and when you log out?

https://askubuntu.com/questions/99232/how-to-make-a-jar-file-run-on-startup-and-when-you-log-out

Here’s a easy way to do that using SysVInit. Instructions:

  1. Create the start and the stop script of your application. Put it on some directory, in our example is:
    • Start Script: /usr/local/bin/myapp-start.sh
    • Stop Script: /usr/local/bin/myapp-stop.sh

    Each one will provide the instructions to run/stop the app. For instance the myapp-start.shcontent can be as simple as the following:

    #!/bin/bash
    
    java -jar myapp.jar 

    For the stop script it can be something like this:

    #!/bin/bash
    # Grabs and kill a process from the pidlist that has the word myapp
    
    pid=`ps aux | grep myapp | awk '{print $2}'`
    kill -9 $pid
  2. Create the following script (myscript) and put it on /etc/init.d.

    /etc/init.d/myscript content:

    #!/bin/bash
    # MyApp
    #
    # description: bla bla
    
    case $1 in
        start)
            /bin/bash /usr/local/bin/myapp-start.sh
        ;;
        stop)
            /bin/bash /usr/local/bin/myapp-stop.sh
        ;;
        restart)
            /bin/bash /usr/local/bin/myapp-stop.sh
            /bin/bash /usr/local/bin/myapp-start.sh
        ;;
    esac
    exit 0
  3. Put the script to start with the system (using SysV). Just run the following command (as root):
    update-rc.d myscript defaults 

PS: I know that Upstart is great and bla bla, but I preffer the old SysV init system.

Script Shell de serviço no linux

O script vai ficar na pasta “/etc/init.d” e depois basta dar um:

sudo nano tomcat7

Depois que abrir o editor basta colar isso:

#!/bin/sh
#
# description: Tomcat Server basic start/shutdown script
# processname: tomcat
 
# Source function library.
if [ -f /etc/rc.d/init.d/functions ];
then
    . /etc/rc.d/init.d/functions
fi
 
 
JAVA_HOME=/usr/java/latest
export JAVA_HOME
 
TOMCAT_HOME=/opt/tomcat/bin
START_TOMCAT=/opt/tomcat/bin/startup.sh
STOP_TOMCAT=/opt/tomcat/bin/shutdown.sh
VERSION_TOMCAT=/opt/tomcat/bin/version.sh
 
 start() {
     echo -n "Starting tomcat: "
     cd $TOMCAT_HOME
     ${START_TOMCAT}
     echo "done."
 }
 
 stop() {
     echo -n "Shutting down tomcat: "
     cd $TOMCAT_HOME
     ${STOP_TOMCAT}
     echo "done."
 }
 
 case "$1" in
          start)
             start
           ;;
 
          stop)
             stop
           ;;
 
         restart)
           stop
           sleep 10
           start
           ;;
         *)
           echo "Usage: $0 {start|stop|restart}"
 esac
 exit 0

Site de referência: http://askubuntu.com/questions/223944/how-to-automatically-restart-tomcat7-on-system-reboots/224402