다른 사용자로 명령을 시작할 수 있는 시스템 서비스 파일을 작성해 보세요.

다른 사용자로 명령을 시작할 수 있는 시스템 서비스 파일을 작성해 보세요.

외부 파일에 정의된 사용자로 명령을 실행할 수 있는 일반 시스템 서비스 파일을 갖는 방법을 찾고 있습니다. 이를 통해 다른 사용자로 다른 서버 인스턴스를 시작할 수 있지만 동일한 버전이 여러 개 있을 수는 없습니다.

나는 노력했다

[Service]
Type=forking
# Specify the level of the myserver instances on this host.
EnvironmentFile=/data/myserver/instances/LEVEL
EnvironmentFile=/data/myserver/instances/%i/USER
ExecStart=/sbin/daemonize /data/myserver/bin/start.sh %i ${LEVEL}
ExecStop=/data/myserver/bin/stop.sh %i
User=$USER

환경 파일에 이것을 가지고 있습니다

USER=user1

환경 파일을 읽기 전에 $USER가 평가되는 것 같습니다. 다음과 같은 것이 허용됩니까?

[Service]
Type=forking
# Specify the level of the myserver instances on this host.
EnvironmentFile=/data/myserver/instances/LEVEL
EnvironmentFile=/data/myserver/instances/%i/USER
ExecStart=/sbin/daemonize /bin/sudo -u $USER /data/myserver/bin/start.sh %i ${LEVEL}
ExecStop=/data/myserver/bin/stop.sh %i
User=root

답변1

Systemd는 템플릿화된 서비스 단위 파일을 사용하여 인스턴스화된 서비스를 지원합니다. "서비스 템플릿"을 참조하십시오.https://www.man7.org/linux/man-pages/man5/systemd.service.5.html.

일반적인 예는 VNC 서비스 템플릿 단위 파일(TigerVNC 서버에 의해 설치됨)입니다. 다음은 OpenVPN의 예입니다.https://fedoramagazine.org/systemd-template-unit-files/

답변2

내가 결국 한 일은

[Service]
Type=forking
# Run as user1 by default
Environment='USER=user1'
# Specify the level of the myserver instances on this host.
EnvironmentFile=/data/myserver/instances/LEVEL
# Allow the specific instance to override the level
EnvironmentFile=-/data/myserver/instances/%i/LEVEL
# Possibly override who this runs as
EnvironmentFile=-/data/myserver/instances/%i/USER
ExecStart=/sbin/daemonize -u $USER /data/myserver/bin/start.sh %i ${LEVEL}
ExecStop=/bin/su - $USER -c '/data/myserver/bin/stop.sh %i'
User=root

관련 정보