Crontab周四定时任务异常求助:配置周四23:55执行重启脚本却在周一或周二触发
Hey there, let's get this sorted out quickly—your current crontab entry is running every single day at 11:55 PM, which explains why it's triggering on Mondays/Tuesdays instead of only Thursdays.
The Root Cause
Crontab follows a 5-field schedule structure (plus the command to run):
分钟 小时 日 月 星期几 命令
Your existing entry 55 23 * * * /usr/html/scheduler/restart.sh > /usr/html/scheduler/file.log uses a * in the 5th field (day of week), which translates to "every day". That's why it's firing daily instead of just your intended Thursdays.
The Correct Crontab Entry
To restrict the task to only Thursdays, set the 5th field to 4 (note: in most crontab implementations, 0=Sunday, 1=Monday, ..., 4=Thursday, 7=Sunday). Here's the fixed line:
55 23 * * 4 /usr/html/scheduler/restart.sh >> /usr/html/scheduler/file.log 2>&1
I added two small but useful tweaks:
- Changed
>to>>to append log entries instead of overwriting the file each time (so you can keep a full history of runs) - Added
2>&1to capture both standard output and error messages in the log—this makes it way easier to debug if the script fails silently
Additional Troubleshooting Steps
If you still hit issues after updating the entry:
- Confirm the crontab was saved properly with
crontab -l(this lists all your scheduled tasks) - Check system cron logs (usually at
/var/log/cronor/var/log/syslog) to verify the task is being triggered on Thursdays - Ensure your script has execute permissions: run
chmod +x /usr/html/scheduler/restart.sh - Test the script manually first to confirm it works as expected:
/usr/html/scheduler/restart.sh
内容的提问来源于stack exchange,提问作者Elaine Byene

