为什么 Linux Shell 脚本中的日期没有更新?
Why date is not updated in Linux Shell Script?
我创建了一个 shell 脚本来创建 cron,以每 5 分钟清理一次 ram 缓存,并在 cron 工作时记录到日志文件 cron_test。我的脚本如下。但是,当我 fisrt 运行 这个脚本时,日期戳仍然存在,它没有更新!例如,如果我 运行 在 2021 年 2 月 1 日星期一 11:47:55 下午的脚本下方,那么它会在每个时间写“clean_ram worked @ Mon 01 Feb 2021 11:47:55 PM EST”线!如何将日期更新为 cron 成功运行的时间?
#!/bin/sh
cat <<EOF1 > /home/clean_ram.sh
#!/bin/sh
exec sudo -s <<'EOF'
sync; echo 1>/proc/sys/vm/drop_caches # clean ram cache
echo "clean_ram worked @ `date`" >> /home/cron_test
EOF1
echo "EOF" >> /home/knoppix/clean_ram.sh # add EOF to the end of clean_ram.sh file manually
# make it exevutable
chmod a+x /home/knoppix/clean_ram.sh
echo "[IS4] creat cron list in crontab"
(crontab -l; echo "*/5 * * * * /home/clean_ram.sh"| crontab -
您正在使用内插 heredoc,因此
`date`
在扩展 heredoc 时进行插值。只需引用定界符即可防止出现这种情况。另外,现在不是 1992 年。使用 $()
:
cat << 'EOF1' > /home/clean_ram.sh
#!/bin/sh
exec sudo -s <<'EOF'
sync; echo 1>/proc/sys/vm/drop_caches # clean ram cache
echo "clean_ram worked @ $(date)" >> /home/cron_test
EOF1
我创建了一个 shell 脚本来创建 cron,以每 5 分钟清理一次 ram 缓存,并在 cron 工作时记录到日志文件 cron_test。我的脚本如下。但是,当我 fisrt 运行 这个脚本时,日期戳仍然存在,它没有更新!例如,如果我 运行 在 2021 年 2 月 1 日星期一 11:47:55 下午的脚本下方,那么它会在每个时间写“clean_ram worked @ Mon 01 Feb 2021 11:47:55 PM EST”线!如何将日期更新为 cron 成功运行的时间?
#!/bin/sh
cat <<EOF1 > /home/clean_ram.sh
#!/bin/sh
exec sudo -s <<'EOF'
sync; echo 1>/proc/sys/vm/drop_caches # clean ram cache
echo "clean_ram worked @ `date`" >> /home/cron_test
EOF1
echo "EOF" >> /home/knoppix/clean_ram.sh # add EOF to the end of clean_ram.sh file manually
# make it exevutable
chmod a+x /home/knoppix/clean_ram.sh
echo "[IS4] creat cron list in crontab"
(crontab -l; echo "*/5 * * * * /home/clean_ram.sh"| crontab -
您正在使用内插 heredoc,因此
`date`
在扩展 heredoc 时进行插值。只需引用定界符即可防止出现这种情况。另外,现在不是 1992 年。使用 $()
:
cat << 'EOF1' > /home/clean_ram.sh
#!/bin/sh
exec sudo -s <<'EOF'
sync; echo 1>/proc/sys/vm/drop_caches # clean ram cache
echo "clean_ram worked @ $(date)" >> /home/cron_test
EOF1