1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

crontabでは/bin/shで実行される

Posted at

背景

朝からめちゃくちゃ詰まったのでここに備忘録

内容

問題

下記のようなスクリプトをcronで実行しようとしたところ。

door.sh
while true; do
    pin17bffn=/var/tmp/pin17
    pin17=$(gpio -g read 17)
    pin17bf=0

    if [[ -e $pin17bffn ]]; then
        pin17bf=$(cat $pin17bffn)
    fi

    if [[ $pin17 -ne $pin17bf ]]; then
        curl -s -XPOST -d "$pin17" https://xxxx
    fi

    echo $pin17>$pin17bffn

    sleep 1
done

crontab
@reboot nohup /home/pi/door.sh  &

うまく動かない…

よく見るとログが出力されていました。

/home/pi/nohup.out
/home/pi/checkpin2.s: 10: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 6: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 10: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 6: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 10: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 6: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 10: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 6: /home/pi/checkpin2.s: [[: not found
/home/pi/checkpin2.s: 10: /home/pi/checkpin2.s: [[: not found

解決

どうやらシェルスクリプトがうまく解釈されていない?
このErrorで調べたところ

String comparison in bash. [[: not found
https://stackoverflow.com/questions/12230690/string-comparison-in-bash-not-found

そのまんまなページを発見
中身を読むと/bin/bashで実行されていないことが原因

では実際に何で実行されている…?


echo "$SHELL"
>/bin/bash

crontab -e
>*/1 * * * * echo "$SHELL">/var/tmp/shell.log

cat /var/tmp/shell.log 
>/bin/sh

ということで、下記のコードをソースに追加して解決

# !/bin/bash
...
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?