xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Raspberry Pi crontab not work bug All In One

Raspberry Pi crontab not work bug All In One

Raspberry Pi crontab 不执行 bug

bug ❌

# 用户 crontab (pi)
pi@raspberrypi:~/Desktop $ crontab -e
pi@raspberrypi:~/Desktop $ crontab -l

#  用户 crontab (root)
pi@raspberrypi:~/Desktop $ sudo crontab -e
pi@raspberrypi:~/Desktop $ sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

  1. 用户 crontab (⚠️ 没有用户字段 pi )
# 自动化执行脚本 ✅
# 每隔 3 分钟执行一次
# 分钟 小时 天 月 周
#3 * * * * pi sh ./Desktop/gpio.py

# user python 参数
# 3 * * * * pi python ./gpio.py 3
# 绝对路径 python ✅

# 二合一输出 log 🚀
3 * * * * /usr/bin/python /home/pi/Desktop/gpio.py 3 > /home/pi/Desktop/gpio.log.txt 2>&1
# 用户 crontab(⚠️ 没有用户字段 pi )
# 3 * * * * pi /usr/bin/python /home/pi/Desktop/gpio.py 3

  1. 系统 crontab /etc/crontab
# /etc/crontab 系统 crontab ✅

# user python 参数
# 3 * * * * pi python ./gpio.py 3
# 绝对路径 python ✅
# 二合一输出 log 🚀
3 * * * * pi /usr/bin/python /home/pi/Desktop/gpio.py 3 > /home/pi/Desktop/gpio.log.txt 2>&1

问题分析

# 开启 cron log ✅
$ sudo vim /etc/rsyslog.conf
# cron.*				/var/log/cron.log

# 重启 rsyslog 服务
$ sudo /etc/init.d/rsyslog restart
Restarting rsyslog (via systemctl): rsyslog.service.

# 查看日志信息
$ cat /var/log/cron.log
# (CRON) info (No MTA installed, discarding output) ❌ 没有配置邮件服务器

image

安装&配置邮件服务器 email / mail

# ✅ step 1
$ sudo apt-get install postfix
# 选 Local Only

 $ sudo apt-get install postfix
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
下列软件包是自动安装的并且现在不需要了:
  libfuse2
使用'sudo apt autoremove'来卸载它(它们)。
建议安装:
  procmail postfix-mysql postfix-pgsql postfix-ldap postfix-pcre postfix-lmdb postfix-sqlite sasl2-bin
  | dovecot-common postfix-cdb mail-reader ufw postfix-doc
下列【新】软件包将被安装:
  postfix
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 41 个软件包未被升级。
需要下载 1,503 kB 的归档。
解压缩后会消耗 4,146 kB 的额外空间。
获取:1 http://deb.debian.org/debian bullseye/main arm64 postfix arm64 3.5.17-0+deb11u1 [1,503 kB]
已下载 1,503 kB,耗时 51秒 (29.3 kB/s)                                                                               
正在预设定软件包 ...
正在选中未选择的软件包 postfix。
(正在读取数据库 ... 系统当前共安装有 99543 个文件和目录。)
准备解压 .../postfix_3.5.17-0+deb11u1_arm64.deb  ...
正在解压 postfix (3.5.17-0+deb11u1) ...
正在设置 postfix (3.5.17-0+deb11u1) ...
正在添加组"postfix" (GID 124)...
完成。
正在添加系统用户"postfix" (UID 116)...
正在将新用户"postfix" (UID 116)添加到组"postfix"...
无法创建主目录"/var/spool/postfix"。
Creating /etc/postfix/dynamicmaps.cf
正在添加组"postdrop" (GID 125)...
完成。
setting myhostname: raspberrypi.lan
setting alias maps
setting alias database
mailname is not a fully qualified domain name.  Not changing /etc/mailname.
setting destinations: $myhostname, raspberrypi, localhost.localdomain, localhost
setting relayhost: 
setting mynetworks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
setting mailbox_size_limit: 0
setting recipient_delimiter: +
setting inet_interfaces: loopback-only
setting default_transport: error
setting relay_transport: error
setting inet_protocols: all
/etc/aliases does not exist, creating it.
WARNING: /etc/aliases exists, but does not have a root alias.

Postfix (main.cf) is now set up with a default configuration.  If you need to 
make changes, edit /etc/postfix/main.cf (and others) as needed.  To view 
Postfix configuration values, see postconf(1).

After modifying main.cf, be sure to run 'systemctl reload postfix'.

Running newaliases
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /lib/systemd/system/postfix.service.
正在处理用于 man-db (2.9.4-2) 的触发器 ...
正在处理用于 rsyslog (8.2102.0-2+deb11u1) 的触发器 ...

Postfix Configuration ├───────────────────────────┐
                  │ Please select the mail server configuration type that best meets your needs.  │ 
                  │                                                                               │ 
                  │  No configuration:                                                            │ 
                  │   Should be chosen to leave the current configuration unchanged.              │ 
                  │  Internet site:                                                               │ 
                  │   Mail is sent and received directly using SMTP.                              │ 
                  │  Internet with smarthost:                                                     │ 
                  │   Mail is received directly using SMTP or by running a utility such           │ 
                  │   as fetchmail. Outgoing mail is sent using a smarthost.                      │ 
                  │  Satellite system:                                                            │ 
                  │   All mail is sent to another machine, called a 'smarthost', for delivery.    │ 
                  │  Local only:                                                                  │ 
                  │   The only delivered mail is the mail for local users. There is no network.   │ 
                  │                                                                               │ 
                  │ General type of mail configuration:                                           │ 
                  │                                                                               │ 
                  │                           No configuration                                    │ 
                  │                           Internet Site                                       │ 
                  │                           Internet with smarthost                             │ 
                  │                           Satellite system                                    │ 
                  │                           Local only                                          │ 
                  │                                                                               │ 
                  │                                                                               │ 
                  │                     <确定>                       <取消>   

Postfix Configuration ├────────────────────────────────────────────┐
 │ The "mail name" is the domain name used to "qualify" _ALL_ mail addresses without a domain name. This includes  │ 
 │ mail to and from <root>: please do not make your machine send out mail from root@example.org unless             │ 
 │ root@example.org has told you to.                                                                               │ 
 │                                                                                                                 │ 
 │ This name will also be used by other programs. It should be the single, fully qualified domain name (FQDN).     │ 
 │                                                                                                                 │ 
 │ Thus, if a mail address on the local host is foo@example.org, the correct value for this option would be        │ 
 │ example.org.                                                                                                    │ 
 │                                                                                                                 │ 
 │ System mail name:                                                                                               │ 
 │                                                                                                                 │ 
 │ raspberrypi____________________________________________________________________________________________________  │ 
 │                                                                                                                 │ 
 │                                <确定>                                  <取消>

# ✅ step 2
$ sudo dpkg-reconfigure postfix
# 选 Local Only

image

sudo dpkg-reconfigure postfix
setting synchronous mail queue updates: true
mailname is not a fully qualified domain name.  Not changing /etc/mailname.
setting destinations: raspberrypi.lan, raspberrypi, localhost.localdomain, localhost
setting relayhost: 
setting mynetworks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
setting mailbox_size_limit: 0
setting recipient_delimiter: +
setting inet_interfaces: loopback-only
setting inet_protocols: all
WARNING: /etc/aliases exists, but does not have a root alias.

Postfix (main.cf) is now set up with the changes above.  If you need to make 
changes, edit /etc/postfix/main.cf (and others) as needed.  To view Postfix 
configuration values, see postconf(1).

After modifying main.cf, be sure to run 'systemctl reload postfix'.

Running newaliases

Postfix Configuration ├────────────────────────────────────────────┐
 │ Mail for the 'postmaster', 'root', and other system accounts needs to be redirected to the user account of the  │ 
 │ actual system administrator.                                                                                    │ 
 │                                                                                                                 │ 
 │ If this value is left empty, such mail will be saved in /var/mail/nobody, which is not recommended.             │ 
 │                                                                                                                 │ 
 │ Mail is not delivered to external delivery agents as root.                                                      │ 
 │                                                                                                                 │ 
 │ If you already have a /etc/aliases file and it does not have an entry for root, then you should add this        │ 
 │ entry.  Leave this blank to not add one.                                                                        │ 
 │                                                                                                                 │ 
 │ Root and postmaster mail recipient:                                                                             │ 
 │                                                                                                                 │ 
 │ _______________________________________________________________________________________________________________ │ 
 │                                                                                                                 │ 
 │                                <确定>                                  <取消>                                   │ 
 │                                                                                           

Postfix Configuration ├────────────────────────────────────────┐
     │ Please give a comma-separated list of domains for which this machine should consider itself the final   │ 
     │ destination. If this is a mail domain gateway, you probably want to include the top-level domain.       │ 
     │                                                                                                         │ 
     │ Other destinations to accept mail for (blank for none):                                                 │ 
     │                                                                                                         │ 
     │ raspberrypi.lan, raspberrypi, localhost.localdomain, localhost_________________________________________ │ 
     │                                                                                                         │ 
     │                              <确定>                                <取消>                               │ 
     │                                                                                     

Postfix Configuration ├────────────────────────────────────────────┐
  │                                                                                                                │ 
  │ If synchronous updates are forced, then mail is processed more slowly. If not forced, then there is a remote   │ 
  │ chance of losing some mail if the system crashes at an inopportune time, and you are not using a journaled     │ 
  │ filesystem (such as ext3).                                                                                     │ 
  │                                                                                                                │ 
  │ Force synchronous updates on mail queue?                                                                       │ 
  │                                                                                                                │ 
  │                                 <是>                                     <否>                                  │ 
  │                                                                                         


# 重启计划任务
$ sudo /etc/init.d/cron restart

# 查看邮件
$ cat /var/mail/root
# cat: /var/mail/root: 没有那个文件或目录

$ cat /var/mail/pi
# cat: /var/mail/pi: 没有那个文件或目录

开启 cron log

$ cat /etc/rsyslog.conf
# /etc/rsyslog.conf configuration file for rsyslog
#
# For more information install rsyslog-doc and see
# /usr/share/doc/rsyslog-doc/html/configuration/index.html


#################
#### MODULES ####
#################

module(load="imuxsock") # provides support for local system logging
module(load="imklog")   # provides kernel logging support
#module(load="immark")  # provides --MARK-- message capability

# provides UDP syslog reception
#module(load="imudp")
#input(type="imudp" port="514")

# provides TCP syslog reception
#module(load="imtcp")
#input(type="imtcp" port="514")


###########################
#### GLOBAL DIRECTIVES ####
###########################

#
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

#
# Set the default permissions for all log files.
#
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022

#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog

#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf


###############
#### RULES ####
###############

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
#cron.*				/var/log/cron.log
# 开启 cron log ✅
cron.*				/var/log/cron.log
daemon.*			-/var/log/daemon.log
kern.*				-/var/log/kern.log
lpr.*				-/var/log/lpr.log
mail.*				-/var/log/mail.log
user.*				-/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info			-/var/log/mail.info
mail.warn			-/var/log/mail.warn
mail.err			/var/log/mail.err

#
# Some "catch-all" log files.
#
*.=debug;\
	auth,authpriv.none;\
	mail.none		-/var/log/debug
*.=info;*.=notice;*.=warn;\
	auth,authpriv.none;\
	cron,daemon.none;\
	mail.none		-/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg				:omusrmsg:*

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://www.cnblogs.com/xgqfrms/p/15384401.html#5166823



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-04-11 17:19  xgqfrms  阅读(85)  评论(9编辑  收藏  举报