Many developers face this issue: when using @reboot
in crontab to auto-start a Play Framework server in a Vagrant environment, the script fails because the /vagrant/
shared folder isn't mounted yet. The timing of the boot sequence makes traditional methods unreliable.
The most reliable solution is to use Vagrant's built-in provisioning system. Add this to your Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: <<-SHELL
cd /vagrant
nohup ./start-play-server.sh &
SHELL
end
For more control, create a systemd service (Ubuntu 16.04+) or upstart job:
# For systemd (/etc/systemd/system/play.service)
[Unit]
Description=Play Framework Server
After=vagrant.mount
[Service]
WorkingDirectory=/vagrant
ExecStart=/vagrant/start-play-server.sh
Restart=always
User=vagrant
[Install]
WantedBy=multi-user.target
Create a wrapper script that waits for the shared folder:
#!/bin/bash
while [ ! -d /vagrant ]; do
sleep 1
done
cd /vagrant
./start-play-server.sh
- Check
vagrant up --debug
logs - Verify folder permissions:
ls -ld /vagrant
- Test with simple echo commands first
Many developers face this issue when trying to auto-start servers in Vagrant: cron's @reboot executes before the /vagrant shared folder is mounted. This timing mismatch prevents your startup scripts from accessing critical application files.
The most elegant solution is to leverage Vagrant's built-in provisioning triggers. Add this to your Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.provision "shell", run: "always", inline: <<-SHELL
cd /vagrant
nohup ./start-play-server.sh > /var/log/play-server.log 2>&1 &
SHELL
end
For more robust process management, create a systemd service:
[Unit]
Description=Play Framework Server
After=vagrant.mount
[Service]
WorkingDirectory=/vagrant
ExecStart=/vagrant/start-play-server.sh
Restart=always
User=vagrant
[Install]
WantedBy=multi-user.target
Then enable it in your provisioning script:
sudo systemctl enable play-server.service
For older systems without systemd, modify /etc/rc.local:
#!/bin/sh -e
#
# rc.local
sleep 10 # Wait for shared folder mount
cd /vagrant
./start-play-server.sh &
exit 0
- Check mount status with
mount | grep vagrant
- Verify systemd dependencies with
systemctl list-dependencies play-server.service
- Monitor logs using
journalctl -u play-server.service -f