#!/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.
Cron examina todo o tempo as modificações e recarregará aquelas que mudaram. Assim o cron não precisa ser reiniciado sempre que um arquivo crontab é modificado.
sudo service cron reload
ou
/etc/init.d/cron reload
grep CRON /var/log/syslog
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
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.
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.
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.
0 * * * * java -jar -Dlogback.configurationFile=/home/mkyong/logback.xml /home/mkyong/crawler/crawler.jar
https://www.mkyong.com/java/java-cron-job-to-run-a-jar-file
One more option is schedule a job at boot time
crontab -e
Choose an editor to open the cron job file. Append the following with your script name
@reboot path/to/script.sh
In your case
crontab -e
@reboot /home/user/Documents/file.sh
Make sure the script has executable permission.
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:
- 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.sh
content 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
- 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
- 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.
Full Stack Developer, DevOps, CSM e LKU. coutoarmando@gmail.com