Restart systemd service on file change?
I'm new to systemd and would like to know what is the best strategy for restarting my web application whenever I deploy new version.
Currently I start my service on boot and it just sits there, however I would like it to restart automatically whenever I change the files since whenever I push to master on gitlab the runner gets the files, compiles and copies it to correct directory. Then I want to restart it or close it however I don't know how since to restart it requires sudo.
Here is my service file
[Unit]
After=mongod.service
[Service]
WorkingDirectory=/var/app/mywebsite/Web
Environment="HOME=/home/stan"
Environment="DOTNET_CLI_TELEMETRY_OPTOUT=1"
Environment="DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1"
ExecStart=/usr/bin/dotnet run -c Release -p /var/app/mywebsite/Web/project.json
[Install]
WantedBy=multi-user.target
Here is my ci file to be more specific
before_script:
- set DOTNET_CLI_TELEMETRY_OPTOUT=1
- set DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
- set HOME=/home/stan
- cd Web
- dotnet restore
- dotnet build -c Release
- cd ..
copy:
script:
- cp -R ./* /var/app/mywebsite/ # After this I would like to restart the website service
If you mean to reload systemd, scanning for new or changed units then add:
sudo systemctl daemon-reload
- open
sudo visudo
- add
gitlab-runner ALL=(ALL) NOPASSWD: /bin/systemctl restart myapp
- now you can do
sudo /bin/systemctl restart myapp
and it will not prompt for password