如何通过crontab @reboot实现CentOS 7上Redmine开机自启
Hey George, let's get that Redmine autostart sorted out. The issue with your original crontab entry boils down to two key problems: crontab runs in a super minimal environment (so it can't find the bundle command unless you use its full absolute path), and your chained cd && bundle syntax doesn't execute properly without wrapping it in a dedicated shell instance.
Here's how to fix it step by step:
Step 1: Get the absolute path to bundle
First, run this command in your terminal to locate exactly where bundle is installed—crontab won't know about your user's PATH shortcuts:
which bundle
You'll get a path like /usr/local/bin/bundle or /home/your-user/.rbenv/shims/bundle—jot this down, you'll need it.
Step 2: Update your crontab entry
Open your crontab editor with crontab -e, then replace your existing line with one of these reliable options:
Option 1: Direct crontab command (with debug logging)
This wraps the entire command sequence in a bash shell, uses the absolute bundle path, and logs output to help you troubleshoot if something goes wrong:
@reboot /bin/bash -c "cd /var/www/html/redmine && /usr/local/bin/bundle exec rails server webrick -e production -b 192.168.1.109 >> /var/log/redmine_startup.log 2>&1 &"
Swap /usr/local/bin/bundle with the path you found in Step 1. The >> /var/log/redmine_startup.log 2>&1 part sends both regular output and errors to a log file, so you can check /var/log/redmine_startup.log if Redmine fails to start.
Option 2: Use a dedicated startup script (cleaner for complex setups)
- Create a script file, for example
/usr/local/bin/start_redmine.sh:
#!/bin/bash # Switch to Redmine's working directory cd /var/www/html/redmine # Start Redmine with the absolute bundle path /usr/local/bin/bundle exec rails server webrick -e production -b 192.168.1.109 >> /var/log/redmine_startup.log 2>&1 &
Again, replace /usr/local/bin/bundle with your actual path from Step 1.
- Make the script executable:
chmod +x /usr/local/bin/start_redmine.sh
- Add this line to your crontab:
@reboot /usr/local/bin/start_redmine.sh
Step 3: Verify it works
Reboot your server, then check:
- If Redmine is running with
ps aux | grep rails - The log file
/var/log/redmine_startup.logfor any error messages - The crontab logs at
/var/log/cronto confirm the command ran on reboot
Bonus: A more robust alternative (systemd)
While crontab works, using a systemd service is far more reliable for long-running apps like Redmine on CentOS 7. If you're open to switching, create a /etc/systemd/system/redmine.service file with:
[Unit] Description=Redmine Service After=network.target [Service] Type=forking User=your-redmine-user WorkingDirectory=/var/www/html/redmine ExecStart=/usr/local/bin/bundle exec rails server webrick -e production -b 192.168.1.109 & Restart=on-failure [Install] WantedBy=multi-user.target
Then enable it with systemctl enable redmine.service and start it with systemctl start redmine.service. But since you specifically asked for crontab, the steps above should get you up and running.
内容的提问来源于stack exchange,提问作者George

