![두 번째 사용자로 실행되는 쉘 스크립트 만들기](https://linux55.com/image/85307/%EB%91%90%20%EB%B2%88%EC%A7%B8%20%EC%82%AC%EC%9A%A9%EC%9E%90%EB%A1%9C%20%EC%8B%A4%ED%96%89%EB%90%98%EB%8A%94%20%EC%89%98%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%20%EB%A7%8C%EB%93%A4%EA%B8%B0.png)
특정 사용자로 두 개의 서비스를 실행하려는 스크립트를 만들고 있습니다. 스크립트는 사용자로 전환한 다음 프로그램을 실행하고 해당 프로그램을 실행한 후 두 번째 프로그램 실행을 시작해야 합니다.
답변1
sudo
특정 사용자로 명령을 실행하도록 지시할 수 있습니다 .
-u user, --user=user
Run the command as a user other than the default target user
(usually root). The user may be either a user name or a
numeric user ID (UID) prefixed with the ‘#’ character (e.g.
#0 for UID 0).
따라서 스크립트에서 sudo -u USERNAME
다음 두 서비스를 실행하는 데 사용할 수 있습니다.
sudo -u foo command1
sudo -u foo command2
그러나 이는 스크립트 자체를 루트로 실행해야 함을 의미합니다. 그렇지 않으면 sudo
사용자에게 비밀번호를 입력하라는 메시지가 표시됩니다 foo
.
답변2
sudo -u username -s -- "program1 && program2"
또는
sudo -u username bash -c 'program1 && program2'