/etc/cron.d相关使用咨询:Ubuntu16.04下cron.daily文件命名与语法合规性
Let's walk through each of your concerns with your Ubuntu 16.04 /etc/cron.daily setup:
1. Is the filename cron_daily safe/valid?
Absolutely. Ubuntu uses run-parts to execute scripts in /etc/cron.daily, and run-parts has strict rules for which files it will run:
- Filenames can only contain letters, numbers, underscores (
_), and hyphens (-) - No file extensions allowed (extensions will cause
run-partsto skip the file)
Your cron_daily name fits these rules perfectly—run-parts will recognize it as a valid script to execute, so this naming is safe and compliant.
2. Are no file extension and no shebang line compliant?
No file extension:
As noted above, this is actually required for run-parts to process the file, so that's totally fine.
No shebang line:
This is a potential gotcha. By default, run-parts on Ubuntu 16.04 executes scripts using /bin/sh, which is a symlink to dash (a POSIX-compliant shell, not Bash). If your script uses Bash-specific syntax (like arrays, [[ ]] conditionals, or shopt settings), running it with dash will cause errors.
Even if you're sourcing a file that sets up a Bash environment, the lack of a shebang means the parent shell executing your script is still dash. To ensure your script runs in Bash as intended, add the shebang line at the very top:
#!/bin/bash
3. Is variable expansion for drt and rse compliant?
If those variables are properly exported (via export drt rse) in the file you're sourcing, then using them directly in your cron_daily script is fully compliant with Bash syntax.
A critical note here: Cron runs with a very minimal environment. When you source a file, make sure to use an absolute path to it (e.g., /home/youruser/env_vars.sh instead of ./env_vars.sh or ~/env_vars.sh). Relative paths won't work reliably in cron's restricted environment.
Extra Tips for Cron Daily Scripts
- Ensure your script has executable permissions: Run
sudo chmod +x /etc/cron.daily/cron_daily—run-partswon't execute non-executable files. - Check cron logs if you run into issues: Look at
/var/log/syslogfor entries tagged withCRONto debug execution problems.
内容的提问来源于stack exchange,提问作者Arcticooling

