ubuntu16.04 java+nginx 开机自启动;java以普通用户启动 【为生产环境客户亲测】
测试了1天半的shell
testsrv@node150:~$ ps -ef | grep fa testsrv 1327 1 11 21:49 ? 00:00:17 java -jar faceFeature-demo-0.0.1-SNAPSHOT.jar testsrv 1530 1493 0 21:51 pts/0 00:00:00 grep --color=auto fa testsrv@node150:~$ cat /etc/rc.local #!/bin/sh -e # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exec 2> /tmp/rc.local.log # send stderr from rc.local to a log file exec 1>&2 # send stdout to the same log file set -x # tell sh to display commands before execution su testsrv echo `whoami` #export MYHOME=/home/testsrv #export PATH=/home/testsrv/bin:/home/testsrv/.local/bin:/opt/java/jdk1.8.0_231/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin #/bin/sleep 60 && /home/testsrv/face.sh & #sh '/home/testsrv/face.sh' #/home/testsrv/face.sh & #${MYHOME}/face.sh & export JAVA_HOME=/opt/java/jdk1.8.0_231 export CLASSPATH=.:${JAVA_HOME}/lib: export PATH=${JAVA_HOME}/bin:$PATH #cd /opt/face su testsrv -c "export JAVA_HOME=/opt/java/jdk1.8.0_231; export CLASSPATH=.:${JAVA_HOME}/lib:; export PATH=${JAVA_HOME}/bin:$PATH;cd /opt/face;java -jar faceFeature-demo-0.0.1-SNAPSHOT.jar > face.log &" exit 0
注意: 千万不要以root用户去操作普通用户home下的资源;shell一定会报错;例如:
su testsrv -c "export JAVA_HOME=/opt/java/jdk1.8.0_231; export CLASSPATH=.:${JAVA_HOME}/lib:; export PATH=${JAVA_HOME}/bin:$PATH;cd /opt/face;java -jar faceFeature-demo-0.0.1-SNAPSHOT.jar > face.log &" 改为 su testsrv -c "export JAVA_HOME=/opt/java/jdk1.8.0_231; export CLASSPATH=.:${JAVA_HOME}/lib:; export PATH=${JAVA_HOME}/bin:$PATH;cd /home/testsrv/;java -jar faceFeature-demo-0.0.1-SNAPSHOT.jar > face.log &"
一定会错误;
即使chmod 777 testsrv 了也没有用; 大部分错误都是权限问题
cd /opt/face; 不能改为 cd /home/testsrv/ ;会报权限问题
nginx 开机自启动 Ubuntu16.04 sudo apt-get install nginx # 安装 sudo apt-get purge nginx nginx-common # 卸载所有东东,包括删除配置文件。 安装之后自动开机启动的; 如果该坏了; 备份配置文件;卸载重新安装;一定有用;
上边错误的;
nginx 代理userhome 下的资源失效 http://192.168.112.128:10080/3.jpg?aa=cc server { listen 10080; server_name localhost; location / { # root /home/testsrv/client1; index index.html index.htm; } } 失效;【必须登录一次系统,之后才能够访问成功】 server { listen 10080; server_name localhost; location / { # root /home/testsrv/client1; root /opt/face/client1; index index.html index.htm; } } 成功;【不需要任何登录操作,严格意义上的开机自启动】 systemctl start facewatcher journalctl -f或者 journalctl -xe systemctl enable facewatcher.service systemctl disable facewatcher.service systemctl disable nginx.service systemctl daemon-reload jar服务开机自启动 testsrv@node150:~$ cat /etc/systemd/system/facewatcher.service [Unit] Description=facewatcher After=syslog.target network.target [Service] User=testsrv WorkingDirectory=/home/testsrv #ExecStart= /opt/java/jdk1.8.0_231/bin/java -jar /home/testsrv/faceFeature-demo-0.0.1-SNAPSHOT.jar >> /home/testsrv/face.log 2>&1 & ExecStart= /opt/java/jdk1.8.0_231/bin/java -jar /home/testsrv/faceFeature-demo-0.0.1-SNAPSHOT.jar #ExecStart= su testsrv -c "export JAVA_HOME=/opt/java/jdk1.8.0_231;export CLASSPATH=.:${JAVA_HOME}/lib:;export PATH=${JAVA_HOME}/bin:$PATH;cd /opt/face;nohup java -jar faceFeature-demo-0.0.1-SNAPSHOT.jar >> face.log 2>&1 &" SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target