我有一个 java 可执行 jar 文件,我正尝试在终端中运行它。
我需要自动运行该项目,以便每当我启动电脑时 jar 文件就会像系统服务一样运行。
答案1
您可以通过将命令添加到末尾来执行此操作,/etc/rc.local
您必须使用 sudo 访问权限来编辑此文件。
因此这个命令应该对你有用,your_executable.jar
用你实际的 jar 文件替换。
sudo echo 'java -jar your_executable.jar' >> /etc/rc.local
答案2
尝试这个:
#!/bin/sh
#
# chkconfig: 345 50 83
#
### BEGIN INIT INFO
# Short description: Trigger Beamer on system startup
# Description: Trigger blupay beamer on system start up
### END INIT INFO
#Source function library
. /etc/rc.d/init.d/functions
#targets beamer config file
if [ -f /etc/sysconfig/yourapp]; then
source /etc/sysconfig/yourapp
fi
#start httpd in the C locale by default.
YOURAPP_LANG=${YOURAPP_LANG-"java"}
# Path to .jar file.
yourapp=path/path_a/path_b
prog=app
pidfile=${PIDFILE-/var/run/yourapp/app.pid}
lockfile=${LOCKFILE-/var/lock/subsys/app}
RETVAL=0
start() {
echo -n "starting $prog"
LANG=${YOURAPP_LANG}
RETVAL=$?
java -jar ${yourapp}/yourjarfilename.jar
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d $app
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$BEAMER_LANG $app$OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $beamer due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $app -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"app shutdown"
fi
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $app
RETVAL=$?
;;
restart)
stop
start
;;
force-reload|reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart||force-reload|reload|status}"
RETVAL=2
esac
exit $RETVAL
转到/etc/sysconfig/
,创建一个文件yourapp
:添加以下内容:
#configuration file for blupay beamer
#
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
# Add options only if you want to
#OPTIONS=
#
#
# By default, bemaer process is started in the java locale; to
# change the locale in which the server runs, the BEAMER_LANG
# variable can be set.
#
#YOURAPP_LANG=java
#
#
#By default, your app will create a app.pid
#
#PID_FILE=/var/run/app/appp.pid
#
现在您应该能够按如下方式运行它:
sudo service app start
务必用适当的名称app
替换YOURAPP
答案3
比作为系统服务运行更简单的解决方案是使用supervisord
。请参阅完整说明http://supervisord.org/
简洁版本
sudo apt install -y supervisor
> cat /etc/supervisor/conf.d/tick.conf
; tick config file
[program:tick]
command=/tmp/ticker.bash
environment=CTX="bogus-42"
> cat /tmp/ticker.bash
#!/bin/bash
while true ; do
echo "tick CTX='${CTX}' SUPERVISOR_PROCESS_NAME=${SUPERVISOR_PROCESS_NAME} $(date)"
sleep 1
done
# ensure supervisor is running
> sudo systemctl restart supervisor
# ensure tick task is running
> sudo supervisorctl start all
> sudo supervisorctl status
tick RUNNING pid 17946, uptime 0:06:11
> d /var/log/supervisor/*
-rw-r--r-- 1 root root 1607 Nov 22 15:17 /var/log/supervisor/supervisord.log
-rw------- 1 root root 0 Nov 22 15:16 /var/log/supervisor/tick-stderr---supervisor-S4zZDy.log
-rw------- 1 root root 16548 Nov 22 15:19 /var/log/supervisor/tick-stdout---supervisor-5z8GTx.log
> sudo tail -3 /var/log/supervisor/tick-stdout---supervisor-5z8GTx.log
tick CTX='bogus-42' SUPERVISOR_PROCESS_NAME=tick Tue Nov 22 15:20:12 PST 2016
tick CTX='bogus-42' SUPERVISOR_PROCESS_NAME=tick Tue Nov 22 15:20:13 PST 2016
tick CTX='bogus-42' SUPERVISOR_PROCESS_NAME=tick Tue Nov 22 15:20:14 PST 2016
答案4
首先,你必须在你的 ubuntu 中安装 java。
其次,jar 文件不是可以运行的可执行文件,但您需要在启动时运行 java,因此您必须编写一个脚本,使其可执行或将您的命令放在您的/etc/rc.local
文件中:
java -jar file.jar &
exit 0
祝你好运