如何使用 systemd 服務啟動需要 root 的腳本?

如何使用 systemd 服務啟動需要 root 的腳本?

我試圖在 CentOS 中啟動一些事情,但遇到了一些我不知道如何解決的問題。

這是我的.service 檔:

[Unit]
Description="Boot Jenkins slave and SonarQube server"
After=network.target

[Service]
Type=simple
ExecStart=/var/boot.sh
User=root
Group=root

[Install]
WantedBy=default.target

systemctl daemon-reloadsystemctl enable my.service和 之後systemctl start my.service,實際上似乎沒有任何事情發生/起作用。

的輸出systemctl status my.service是:

$ systemctl status my.service 
● startup.service - "Boot Jenkins slave and SonarQube server"
   Loaded: loaded (/etc/systemd/system/my.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2017-04-26 19:49:31 IDT; 1min 41s ago
  Process: 988 ExecStart=/var/boot.sh (code=exited, status=0/SUCCESS)
 Main PID: 988 (code=exited, status=0/SUCCESS)

Apr 26 19:49:00 build-1 systemd[1]: Started "Boot Jenkins slave and SonarQube server".
Apr 26 19:49:00 build-1 systemd[1]: Starting "Boot Jenkins slave and SonarQube server"...
Apr 26 19:49:00 build-1 boot.sh[988]: Starting SonarQube...
Apr 26 19:49:01 build-1 boot.sh[988]: Started SonarQube.

據說,事情應該有效,但它們沒有......
但是,如果我手動運行 boot.sh,它們就會起作用。

su ...
./boot.sh

boot.sh 是:

#!/bin/bash

nohup java -jar /var/jenkins/slave.jar -jnlpUrl https://***.jnlp -secret **** &
/var/sonarqube-6.3/bin/linux-x86-64/sonar.sh start

請注意,SonarQube 需要 root。
我究竟做錯了什麼?

更新:日誌來自cat /var/log/messages | grep SonarQube

Apr 26 20:58:53 build-1 systemd: Started "Boot Jenkis slave and SonarQube server".
Apr 26 20:58:53 build-1 systemd: Starting "Boot Jenkis slave and SonarQube server"...
Apr 26 20:58:53 build-1 boot.sh: Starting SonarQube...
Apr 26 20:58:53 build-1 boot.sh: Started SonarQube.

答案1

對於 SonarQube 我最終只是遵循這個:https://devopscube.com/setup-and-configure-sonarqube-on-linux/,它指示如何將 SonarQube 新增為服務...奇怪的是,這比 systemd 方法簡單得多。

對於文件中的第一個命令,這仍然是一個懸而未決的問題...

相關內容