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:
  1. Modern Debian and Ubuntu systems, which symlink sh to dash by default;
  2. Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.
  3. 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

# add a apt, tailscale list

curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.noarmor.gpg \
| sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.tailscale-keyring.list \
| sudo tee /etc/apt/sources.list.d/tailscale.list

# then install tailsale

sudo apt-get update
sudo apt-get install tailscale

# then start tailscale

ex) tailscale up \
--operator=$USER \
--accept-routes \
--advertise-exit-node \ # tailscale을 설정한 현재 device로 network traffic을 전송 하도록 설정
--advertise-routes=10.0.0.0/24,10.0.2.0/24,172.18.0.0/24,169.254.69.254/32 \ # tailscale을 설정한 device의 네트워크 대역을 tailnet 그룹에 전파 설정
--hostname=$HOSTNAME
--------------------------------------------------
sudo tailscale up

The easiest, most secure way to use WireGuard.
USAGE
  tailscale [flags] <subcommand> [command flags]

For help on subcommands, add --help after: "tailscale status --help".
This CLI is still under active development. Commands and flags will
change in the future.

SUBCOMMANDS
  up          Connect to Tailscale, logging in if needed
  down        Disconnect from Tailscale
  set         Change specified preferences
  login       Log in to a Tailscale account
  logout      Disconnect from Tailscale and expire current node key
  switch      Switches to a different Tailscale account
  configure   [ALPHA] Configure the host to enable more Tailscale features
  netcheck    Print an analysis of local network conditions
  ip          Show Tailscale IP addresses
  status      Show state of tailscaled and its connections
  ping        Ping a host at the Tailscale layer, see how it routed
  nc          Connect to a port on a host, connected to stdin/stdout
  ssh         SSH to a Tailscale machine
  funnel      Serve content and local servers on the internet
  serve       Serve content and local servers on your tailnet
  version     Print Tailscale version
  web         Run a web server for controlling Tailscale
  file        Send or receive files
  bugreport   Print a shareable identifier to help diagnose issues
  cert        Get TLS certs
  lock        Manage tailnet lock
  licenses    Get open source license information
  exit-node   Show machines on your tailnet configured as exit nodes
  update      Update Tailscale to the latest/different version
  whois       Show the machine and user associated with a Tailscale IP (v4 or v6)
  drive       Share a directory with your tailnet
  completion  Shell tab-completion scripts

FLAGS
  --socket value
        path to tailscaled socket (default /var/run/tailscale/tailscaled.sock)

# tail lock 설정

# tail sign 설정

Add signing node
To add signing devices for your network, you can use the use the Tailscale CLI. Run the following command on the new device you'd like to add.

sudo tailscale lock status

< tailscale lock status >

Copy the tailnet lock key, which begins with tlpub:
Then, from an existing signing device, run the following command.

sudo tailscale lock add 

After which, the first device will be marked as a signing device.

sudo tailscale lock add <copied-key>

 

'IT' 카테고리의 다른 글

Crontab 에서 $RANDOM 활용을 위한 대안  (0) 2024.05.26
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
sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

sudo systemctl restart systemd-resolved
sudo systemctl restart NetworkManager
sudo systemctl restart tailscaled

'IT' 카테고리의 다른 글

Crontab 에서 $RANDOM 활용을 위한 대안  (0) 2024.05.26
How to set up tailscale  (0) 2024.05.15
old snap revision delete  (0) 2024.05.15
xauth 설정 및 x11 접속  (0) 2024.05.15
Docker setting  (0) 2023.02.12

+ Recent posts