corntab 에서 $RANDOM을 사용 하려고 하면 반응 하지 않는다.
기본 shell이 sh > dash이기 때문에 나타난 현상이다.
sh와 bash의 차이는 아래와 같다.
더보기
더보기
What is sh?
sh (or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88, Dash, ...). Bash can also be considered an implementation of sh (see below).Because sh is a specification, not an implementation, /bin/sh is a symlink (or a hard link) to an actual implementation on most POSIX systems.
What is Bash?
Bash started as an sh-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself Bash is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.Bash supports a --posix switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as sh.
sh = bash?
For a long time, /bin/sh used to point to /bin/bash on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.
Some popular examples of systems where /bin/sh does not point to /bin/bash (and on some of which /bin/bash may not even exist) are:
- Modern Debian and Ubuntu systems, which symlink sh to dash by default;
- Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.
- BSD systems, and in general any non-Linux systems. OpenBSD uses pdksh, a descendant of the KornShell. FreeBSD's sh is a descendant of the original Unix Bourne shell. Solaris has its own sh which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.
How can you find out what /bin/sh points to on your system?
The complication is that /bin/sh could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:
% file -h /bin/sh
/bin/sh: symbolic link to bash
If it's a hard link, try
% find -L /bin -samefile /bin/sh
/bin/sh
/bin/bash
In fact, the -L flag covers both symlinks and hardlinks, but the disadvantage of this method is that it is not portable — POSIX does not require find to support the -samefile option, although both GNU find and FreeBSD find support it.
cron 설정을 위해 아래와 같이 무작위 시간 대기후 실행을 하고자 할때 sh에서는 랜덤 변수가 정의되어 있지 않기 때문에 정시에 실행이 된다.
crontab
1 * * * * sleep $RANDOM && command.sh
이를 해결 하기 위해서는 sh에서도 확인 가능한 무작위 숫자를 만들어야 한다.
od -An -N4 -tu4 /dev/urandom: /dev/urandom에서 4바이트를 읽어 부호 없는 4바이트 정수로 해석하여 출력
$((...)): 산술 연산 수행
\% : crontab 내에서 % 는 줄바꿈을 의미하여, 연산자로 인식 하도록 "\" 포함한다.
위의 사항을 적용한 결과는 아래와 같다.
corntab for sh(dash)
1 * * * * sleep $(($(od -An -N4 -tu /dev/urandom) \% 59))m && commnad.sh
물론.... 이렇게 해도 된다.
corntab
1 * * * * /bin/bash sleep $(($RANDOM \% 59))m && commnad.sh
끝!
'IT' 카테고리의 다른 글
| How to set up tailscale (0) | 2024.05.15 |
|---|---|
| Configuring Linux DNS (0) | 2024.05.15 |
| old snap revision delete (0) | 2024.05.15 |
| xauth 설정 및 x11 접속 (0) | 2024.05.15 |
| Docker setting (0) | 2023.02.12 |