재부팅 후 명령/스크립트를 한 번 실행하는 방법

재부팅 후 명령/스크립트를 한 번 실행하는 방법

Centos6에는 터미널이 다시 시작된 후 한 번 실행해야 하는 이 스크립트가 있습니다.
어떻게 해야 하나요?
이렇게 실행하면 sh /path/to/script.sh모든 것이 정상이지만 rc.local( sh /path/to/script.sh) 또는 crontab( @reboot sh /path/to/script.sh)를 추가하면 아무 일도 일어나지 않습니다.
어떤 도움이라도 기꺼이 제공해 드리겠습니다.

#!/bin/bash
gnome-terminal -x sh -c 'zenity --info --text="Msg1" --title="Text1..." --timeout=10
<some_command>
zenity --info --text="Msg2" --title="Text2..."  --timeout=10
<some_command>
zenity --info --text="Msg3" --title="Reboot..."  --timeout=10
sleep 1
exec bash'

답변1

Gnome Terminal은 X 애플리케이션(GUI 애플리케이션)입니다. cron에서 X 응용 프로그램을 실행하려면 어떤 모니터를 사용하고 있는지 "알려주세요". cron일반 쉘 환경에서는 명령이 실행되지 않기 때문입니다.

먼저 시스템에서 어떤 모니터가 사용되고 있는지 검색하십시오.

echo $DISPLAY

출력은 다음과 같습니다:

:0

또는

:1

DISPLAY변수가 :1GUI 응용 프로그램 변수를 사용하여 명령 앞에 스크립트에 추가되었다고 가정합니다 DISPLAY=:1. 즉:

#!/bin/bash
DISPLAY=:1 gnome-terminal -x sh -c 'zenity --info --text="Msg1" --title="Text1..."  --timeout=10;<some_command>;zenity --info --text="Msg2" --title="Text2..."  --timeout=10;<some_command>;zenity --info --text="Msg3" --title="Reboot..."  --timeout=10;sleep 1; exec bash'

이 외에도 CentOS에는 시스템 시작 시 무언가를 한 번 실행할 수 있는 또 다른 가능성, 즉 서비스 메커니즘이 cron있습니다 . rc-local파일을 생성합니다(아직 생성되지 않은 경우).

/etc/rc.d/rc.local

콘텐츠:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/path/to/script.sh

exit 0

시작 시 실행하려는 모든 명령을 이 파일에 넣으세요.

rc.local파일을 실행 가능 하게 만듭니다 .

chmod +x /etc/rc.d/rc.local

rc-local서비스를 활성화 하고 시작합니다.

systemctl enable rc-local
systemctl start rc-local

서비스가 제대로 실행되고 있는지 확인하세요.

systemctl status rc-local

관련 정보