programing tip

* nix에서 로그인 할 때 스크립트를 어떻게 실행합니까?

itbloger 2020. 11. 7. 09:05
반응형

* nix에서 로그인 할 때 스크립트를 어떻게 실행합니까?


나는 이것을 한 번 알고 있지만 ... 유닉스에서 로그인 할 때 스크립트를 어떻게 실행합니까 (bash는 괜찮습니다)?


에서 위키 피 디아 배쉬

Bash가 시작되면 다양한 스크립트에서 명령을 실행합니다.

Bash가 대화 형 로그인 쉘로 호출 될 때 먼저 / etc / profile 파일에서 명령을 읽고 실행합니다 (해당 파일이있는 경우). 해당 파일을 읽은 후 ~ / .bash_profile, ~ / .bash_login 및 ~ / .profile을 순서대로 찾고 존재하고 읽을 수있는 첫 번째 명령부터 읽고 실행합니다.

로그인 셸이 종료되면 Bash는 ~ / .bash_logout 파일 (있는 경우)에서 명령을 읽고 실행합니다.

로그인 셸이 아닌 대화 형 셸이 시작되면 Bash는 해당 파일이있는 경우 ~ / .bashrc에서 명령을 읽고 실행합니다. --norc 옵션을 사용하여 금지 할 수 있습니다. --rcfile 파일 옵션은 Bash가 ~ / .bashrc 대신 파일에서 명령을 읽고 실행하도록합니다.


로그인시 대부분의 셸은 사용자 지정 스크립트를 실행하는 데 사용할 수있는 로그인 스크립트를 실행합니다. 셸이 실행하는 로그인 스크립트는 물론 셸에 따라 다릅니다.

  • bash : .bash_profile, .bash_login, .profile (이전 버전과의 호환성을 위해)
  • sh : .profile
  • tcsh 및 csh : .login
  • zsh : .zshrc

다음을 수행하여 사용중인 쉘을 찾을 수 있습니다.

echo $SHELL

프롬프트에서.

'로그인'에 대한 약간 더 넓은 정의를 위해 X가 시작될 때 대부분의 배포판에서 X 세션이 시작될 때 .xsessionrc가 실행된다는 것을 아는 것이 유용합니다.


강타의 첫 번째를 사용하는 경우 ~/.bash_profile, ~/.bash_login그리고 ~/.profile대화 형 로그인 쉘 실행됩니다. 나는 ~/.profile일반적으로 Bash 외에 Unix 쉘에 의해 실행 된다고 생각 합니다. Bash는 ~/.bashrc비 로그인 대화 형 쉘에 대해 실행 됩니다.

일반적으로 항상 설정하려는 모든 항목을 넣은 .bashrc다음에서 실행합니다. .bash_profile여기서도 설정 ssh-agent또는 실행 과 같이 로그인 할 때만 실행해야하는 몇 가지 항목을 설정합니다 screen.


하나의 스크립트와 하나의 스크립트 실행하려면 사용자가 기본 셸로 만들 수 있습니다.

echo "/usr/bin/uptime" >> /etc/shells
vim /etc/passwd  
  * username:x:uid:grp:message:homedir:/usr/bin/uptime

흥미로운 효과를 가질 수 있습니다 :) (안전하지 않으므로 너무 많이 신뢰하지 마십시오. 기본 쉘을 드라이브를 지우는 스크립트로 설정하는 것과 같은 것은 없습니다. ...하지만, .. 시나리오를 상상할 수 있습니다. 놀랍도록 유용 할 수 있습니다)


OSX를 사용하는 경우 ~/.profile


bash 프로필 에 넣으십시오 .

~/.bash_profile

Launchd OS X에서 선호되는 방법입니다.

로그인에서 실행하려면 입력하십시오. ~/Library/LaunchAgents

시작 launchd항목

launchctl load /Library/LaunchDaemons/com.bob.plist

중지 항목

launchctl unload /Library/LaunchDaemons/com.bob.plist

com.bob.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bob</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>-jar</string>
<string>/Users/user/program.jar</string>
</array>
</dict>
</plist>

이 문제로 며칠 동안 좌절했습니다. 우분투에서는 아무것도 작동하지 않았습니다. / etc / profile에 전화를 걸면 로그인 시도시 모두 충돌했습니다. 내가 원하는 것이 아니기 때문에 "시작 응용 프로그램"을 사용할 수 없습니다. 현재 사용자에 대한 스크립트 만 설정합니다.

Finally I found this little article: http://standards.freedesktop.org/autostart-spec/autostart-spec-0.5.html

The solution would be:

  1. find out the $XDG_CONFIG_DIRS path:

    echo $XDG_CONFIG_DIRS

  2. put your script in that directory


Add an entry in /etc/profile that executes the script. This will be run during every log-on. If you are only doing this for your own account, use one of your login scripts (e.g. .bash_profile) to run it.


Search your local system's bash man page for ^INVOCATION for information on which file is going to be read at startup.

man bash
/^INVOCATION

Also in the FILES section,

   ~/.bash_profile
          The personal initialization file, executed for login shells
   ~/.bashrc
          The individual per-interactive-shell startup file

Add your script to the proper file. Make sure the script is in the $PATH, or use the absolute path to the script file.


The script ~/.bash_profile is run on login.

참고URL : https://stackoverflow.com/questions/97137/how-do-you-run-a-script-on-login-in-nix

반응형