Arquivo da tag: Crontab

Dando alto start no Tomcat caso esteja fora do AR

#!/bin/bash
r=`wget -q localhost:8080`
if [ $? -ne 0 ]
  then /opt/tomcat/bin/startup.sh
  else echo "OK"
fi

Depois de criar esse arquivo dê permissão de executável.

sudo chmod +x arquivo.sh

Para finalizar coloque ele para ser chamado no crontab.

Amazon – Fazendo as máquinas do EC2 dormirem

Primeiro instale a linha de commando: Linha de Comando

Ligando às 19h:
0 19 * * * ~/start_instances.sh.

# start_instances.sh
 
export AWS_ACCESS_KEY="UTHEENDZNAPPA20HAI11"
export AWS_SECRET_KEY="eij5ugaiphee6uusheg6eiVaiD0ein8moh7ieS0o"
 
aws ec2 start-instances i-912345ab i-812345bc

Desligando às 7h:
0 7 * * * ~/stop_instances.sh.

# stop_instances.sh
 
export AWS_ACCESS_KEY="UTHEENDZNAPPA20HAI11"
export AWS_SECRET_KEY="eij5ugaiphee6uusheg6eiVaiD0ein8moh7ieS0o"
 
aws ec2 stop-instances i-912345ab i-812345bc

Exemplo de CRONTAB:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
0 7 * * * ~/stop_instances.sh
0 19 * * * ~/start_instances.sh

Referência

Linux – Execute java cron job to run a jar file

1. Create a Cron Job

To create a cron job, in terminal, type crontab -e to edit the cron job. Review following examples :

1.1 Run daily at 00:00, accepts two parameters.

# run everyday
0 0 * * * java -jar /home/mkyong/crawler/webcrawler.jar param1 param2

1.2 Run daily at 02:00 am, pass rir.name as a system property with -D option.

# run everyday at 2am
0 2 * * * java -jar -Drir.name="ripe" /home/mkyong/crawler/whoiscrawler.jar

1.3 Run hourly, assume this jar is logging message with the logback framework.

# run every hour
0 * * * * java -jar -Dlogback.configurationFile=/home/mkyong/logback.xml /home/mkyong/crawler/crawler.jar

Java – Cron job to run a jar file

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.