
이 경우 일치 항목(updateKey.sh)을 찾기 전에 줄에 텍스트를 추가하려고 시도했지만 작동하지 못했습니다. 이것은 내 crontab 파일이고 스크립트는 여기에 줄을 추가합니다.
0 06,18 * * * /home/server/scripts/CCgenerator.sh
0 05 * * * /home/server/scripts/updateKey.sh
첫 번째 줄인 "CCgenerator.sh"가 가끔 제거되지만, 그렇게 보일 것입니다. 다음은 해당 줄을 추가하는 스크립트입니다.
#!/bin/bash
CCgenerator="0 06,18 * * * /home/server/scripts/CCgenerator.sh"
updateKey="0 05 * * * /home/server/scripts/updateKey.sh"
if ! sudo grep -q "$CCgenerator" /var/spool/cron/crontabs/root; then
echo "Adds CCgenerator.sh"
sudo sed -i '/\$updateKey/i $CCgenerator' /var/spool/cron/crontabs/root
else
echo "CCgenerator.sh found"
fi
exit
답변1
cron 편집 목적의 경우 l0b0의 답변이 다음과 같은 스크립트를 수정하는 가장 좋은 방법입니다.
- 검색 키의 이스케이프 점 및 별표(
updateKey
) - 대체 구분 기호 사용
sed
(내 선택%
) - sed 표현식 주위의 큰따옴표(파싱하려는 bash 변수)
#!/bin/bash
CCgenerator="0 06,18 * * * /home/server/scripts/CCgenerator.sh"
updateKey="0 05 \* \* \* /home/server/scripts/updateKey\.sh"
if ! grep -q "$CCgenerator" cron; then
echo "Adds CCgenerator.sh"
sed -i "\%$updateKey%i $CCgenerator" cron
else
echo "CCgenerator.sh found"
fi
exit
답변2
crontab을 직접 업데이트하고 싶지 않고 대신크론 작업을 위한 표준 메커니즘 추가:
line="* * * * * /path/to/command"
(crontab -u userhere -l; echo "$line" ) | crontab -u userhere -
답변3
crontab 파일을 직접 편집하지 마십시오. 명령을 사용하세요
crontab
. 그게 바로 그 목적입니다.sudo
스크립트에서 여러 번 사용 하지 마십시오 . 전체 스크립트를 실행하려면 sudo를 사용하세요.변수와 문자열을 올바르게 인용하세요. 리터럴 문자열은 작은따옴표로 묶고 변수와 같은 보간은 큰따옴표로 묶습니다.
정규식보다는 고정 문자열을 grep해야 하므로 를 사용하십시오
grep -F
. 그렇지 않으면*
패턴의 s가 "0개 이상의 공백"으로 해석됩니다.특정 반환 값을 설정하려는 경우가 아니면(귀하가 설정하지 않은)
exit
스크립트 끝에 이를 추가할 필요가 없습니다. 어쨌든 스크립트는 결국 종료됩니다.CCgenerator.sh
crontab 항목이 존재하는지 확인하고 싶지만updateKey.sh
존재한다고 가정하십시오.더 중요한 것은 왜 crontab에서 규칙이 어디에 있는지 관심을 두는 것입니까
CCgenerator.sh
? 파일의 시작 부분이나 끝 부분에 있어도 효과는 동일합니다. 그러니 전혀 찾아보실 필요가 없습니다updateKey.sh
.
이렇게 하면 위의 모든 문제가 해결됩니다.
#! /bin/bash
CCgenerator='0 06,18 * * * /home/server/scripts/CCgenerator.sh'
if ! crontab -u root -l | grep -Fq "$CCgenerator" ; then
echo 'Adding CCgenerator.sh'
# append "$CCgenerator to end of root's crontab
(crontab -u root -l ; printf '%s\n' "$CCgenerator") | crontab -u root
else
echo 'CCgenerator.sh found'
fi
전체 스크립트를 실제로 실행하지 않으려면 명령을 사용할 때마다 스크립트를 추가하세요 sudo
.sudo
crontab
선을 CCGenerator.sh
기준으로 선의 위치 에 정말로 관심이 있다면 다음을 수행하세요.updateKey.sh
#! /bin/bash
CCgenerator='0 06,18 * * * /home/server/scripts/CCgenerator.sh'
updateKey='0 05 \* \* \* /home/server/scripts/updateKey\.sh'
if ! crontab -u root -l | grep -Fq "$CCgenerator" ; then
echo 'Adding CCgenerator.sh'
crontab -u root -l | sed -e "\:$updateKey: i\
$CCgenerator
" | crontab -u root
else
echo 'CCgenerator.sh found'
fi
$updateKey
그러나 에 없으면 아무 작업도 수행되지 않습니다 crontab
. 더 나은 버전은 grep for 이고 $updateKey
, 존재하는 경우 sed
삽입하는 명령을 실행하고, 그렇지 않으면 crontab 끝에 추가하는 데 사용하는 $CCgenerator
하위 쉘과 같은 것을 사용하는 것입니다 .crontab -u root -l ; printf ...
$CCgenerator
어쩌면 다음과 같은 것일 수도 있습니다.
#! /bin/bash
CCgenerator='0 06,18 * * * /home/server/scripts/CCgenerator.sh'
updateKey='0 05 \* \* \* /home/server/scripts/updateKey\.sh'
# we're going to use `crontab -u root -l` multiple times, it's
# best to just fetch it once and store it in a variable.
rootcrontab="$(crontab -u root -l)"
if ! grep -Fq "$CCgenerator" <<<"$rootcrontab"; then
if grep -q "$updateKey" <<<"$rootcrontab" ; then
echo 'Inserting CCgenerator.sh'
echo "$rootcrontab" | sed -e "\:$updateKey: i\
$CCgenerator
" | crontab -u root
else
echo 'Appending CCgenerator.sh'
printf '%s\n' "$rootcrontab" "$CCgenerator" | crontab -u root
fi
else
echo 'CCgenerator.sh found'
fi
참고: $updateKey
해당 *
문자는 이미 이스케이프 처리되어 있으므로 를 사용할 필요가 없습니다 grep -F
.