I want to run a .jar file always after I booted up my Raspberry Pi. I know how to run the file in the console:
java -jar pi/test.jar
But how can I save this command in a executable file and where is the place to save it, that the file starts after boot up? This is not a duplicate because it's different on a Raspberry Pi than on other linux systems.
you can use my setup :
save this in /etc/init.d/raspberryUtils
(change as appropriate)
#!/bin/bash
# MyApp
#
# description:raspberryUtils util service
case $1 in
start)
/bin/bash /home/developer/raspberryUtils/bootstartup/startServer.sh
;;
stop)
/home/developer/raspberryUtils/bootstartup/stopServer.sh
;;
restart)
/home/developer/raspberryUtils/bootstartup/stopServer.sh
/home/developer/raspberryUtils/bootstartup/startServer.sh
;;
esac
exit 0
startServer.sh:
#!/bin/bash
java -cp /home/developer/raspberryUtils/dist/RaspberryUtils.jar service.StartServices &
stopServer.sh
#!/bin/bash
# Grabs and kill a process from the pidlist that has the word myapp
pid=`ps aux | grep RaspberryUtils | awk '{print $2}'`
kill -9 $pid
this way you can stop,start without restarting the pi too
NOTE:
as mentioned by @Cosu its better using jps
so stopServer.sh is :
#!/bin/bash
# Grabs and kill a process from the pidlist that has the word StartService
pid=`jps | grep StartService | awk '{print $1}'`
kill -9 $pid
You can user crontab for executing jar file every time you reboot. In terminal type crontab -e. at last of that code add line "@reboot command(For jar)". If crontab -e is not working properly that execute export EDITOR=nano and then crontab -e.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.