I was told that it would be better to make it a service with systemd, so I tried it for the time being.
① Go build with the project file (sample in this case) ② Create app.service in etc / systemd / system ③ Describe the following in app.service
[Unit]
Description=Automatic start server demon
[Service]
ExecStart=/home/sample/sample
WorkingDirectory=/home/ec2-user/sample
Restart=always
Type=simple
User=ec2-user
[Install]
WantedBy=multi-user.target
[Explanation] Specify the executable file with ExecStart When go build, the same binary as the file name will be generated, so specify it
Specify the directory to be executed in Working Directory
By setting Restart = always always, it will restart automatically even if it fails.
Type = simple Specify the timing to determine that execution is complete
simple = When the command is executed Type = forking = When the executed command is finished When the Type = oneshot = command completes
User User to execute
Create a link in the .wants directory of this unit when WantedBy = multi-user.target is enabled I wasn't sure about this.
4 Run `` `sudo systemctl daemon -reload``` Updated the contents of app.service
⑤ Execute `` `sudo systemctl enable /home/ec2-user/etc/systemd/system/app.service``` Enable automatic service startup
⑥ Execute sudo systemctl start app.service
This will start automatically
Recommended Posts