20日ごとに電子メールを送信するシェルスクリプトが必要です。

20日ごとに電子メールを送信するシェルスクリプトが必要です。

誰かが私に20日ごとにメールを送信するシェルスクリプトを書くロジックまたはスクリプト全体を教えてもらえますか?

ベストアンサー1

テキストのみで添付ファイルがないメールの場合:

/some/path/script.sh

コンテンツ:

#send me an email:
cat text | mail -s "subject" [email protected]

20日ごとにインポートするには、スクリプトを次のように追加するように変更することが1つの方法です。

#send me an email:
cat text | mail -s "subject" [email protected]
#and the script itself re-schedule itself to start again in 20 days:
echo "$0" | at now + 20 days #And remember to launch that script using its full path

別の方法はcrontabを使用することです(crontab -eスクリプトを実行したいユーザーとして)。 crontabで20日ごとにスクリプトを実行しますman crontab^^

その電子メールの添付ファイルが必要な場合は、「mutt」(またはコマンドラインで使いやすく、添付ファイルを処理できるような同様のプログラム)をインストールすることをお勧めします。

===

今、私は毎日cronを使用したいと明示的に述べています。もう1つのアプローチ:毎日スクリプトを実行し、20日後に特別なタスク(電子メールを送信するなど)を実行したいですか?

1つの方法は、この部分をスクリプトに入れることです。

#the script

#near the beginning:
[ ! -e /some/flagfile ] && touch /some/flagfile #create /some/flagfile, ONCE.

...  #the usual script treatment, if any

#and the test: if our flagfile is >=20 days old, we mail a msg and delete the flag
sleep 10  #IMPORTANT: that way we are sure the flag done 20 days ago is at least
          #           20days+a few seconds, and thus the following test will work !
if ( find /some -mtime +20 -print | grep '/some/flagfile$' >/dev/null )
then
     # we found a /some/flagfile of at least 20 days!
    cat /some/message | mail -s "subject" [email protected]
    rm /some/flagfile  #you could add checks that the email worked...
     # so next time you run the script, it will create the new /some/flagfile.
     # But if you prefer to have the 20 days start "now" instead of when the script
     # is run next, you could uncomment the next line instead:
    #touch /some/flagfile
fi

...

おすすめ記事