Linux 設置nginx 以及java jar自啟動
linux 設置nginx 自啟動
sudo vim /etc/systemd/system/nginx.service 在文件中添加以下內容(根據你的JAR文件路徑和用戶需求進行調整) [Unit] Description=The NGINX HTTP and reverse proxy server After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target 重新加載systemd,啟用并啟動你的服務: sudo systemctl daemon-reload sudo systemctl start nginx sudo systemctl enable nginx
linux 設置java jar 自啟動
在Linux系統中,要讓Java的JAR文件自動啟動,你可以通過幾種方法來實現。以下是一些常見的方法: 1. 使用nohup和& 你可以在終端中使用nohup命令來運行你的JAR文件,并使用&將其置于后臺運行。這樣即使你關閉了終端,程序也會繼續運行。 nohup java -jar your-application.jar & 2. 使用screen或tmux screen或tmux是終端復用器,它們允許你啟動一個或多個會話,并在這些會話中運行程序。即使你斷開連接,會話也會繼續運行。 首先,安裝screen或tmux(如果尚未安裝): sudo apt-get install screen # 對于Debian/Ubuntu sudo yum install screen # 對于CentOS/RHEL sudo apt-get install tmux # 對于Debian/Ubuntu sudo yum install tmux # 對于CentOS/RHEL 然后,使用以下命令啟動一個新會話并運行你的JAR文件: screen -S your-session-name -d -m java -jar your-application.jar # 或者使用 tmux tmux new -s your-session-name -d 'java -jar your-application.jar' 3. 使用systemd服務 對于更高級的自動啟動和管理,你可以創建一個systemd服務。這樣,你可以輕松地通過systemctl命令來啟動、停止和管理你的服務。 創建一個新的服務文件: sudo nano /etc/systemd/system/your-application.service 在文件中添加以下內容(根據你的JAR文件路徑和用戶需求進行調整): [Unit] Description=Your Java Application Service After=network.target [Service] User=your-user ExecStart=/usr/bin/java -jar /path/to/your-application.jar SuccessExitStatus=143 Restart=on-failure Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 [Install] WantedBy=multi-user.target 重新加載systemd,啟用并啟動你的服務: sudo systemctl daemon-reload sudo systemctl enable your-application.service sudo systemctl start your-application.service 4. 使用cron定時任務 如果你希望在特定時間自動啟動JAR文件,可以使用cron定時任務。 編輯cron任務: crontab -e 添加一行來指定任務在特定時間運行,例如每天凌晨1點: 0 1 * * * /usr/bin/java -jar /path/to/your-application.jar > /path/to/logfile.log 2>&1 確保根據你的實際路徑和需求調整這些命令。這些方法中的每一種都可以幫助你實現在Linux上自動啟動Java JAR文件的需求。選擇最適合你的場景的方法。

浙公網安備 33010602011771號