On AWS or any other cloud platform, we can enable our NextJS app to be automatically started through a cron job. This cron job will be executed every time when the server (or instance) is rebooted.
For the rest of the tutorial, assume that our NextJS app is contained in a directory test-app that is present in the home directory.
Create Bash Script to Hold NextJS App Start Commands
Create a bash script, as an example aws.sh.
sudo nano aws.sh
-
Enter commands for going to the path of the code directory and execute pm2 command.
cd test-app pm2 start npm --name "TestApp" -- start
Give execute permissions to the created bash script.
chmod +x aws.sh
Create the Cron Job
Open the cron job file. The terminal may give an option to create it if never created before.
crontab -e
Enter the command that will run the earlier created bash script on reboot.
@reboot ./aws.sh