10

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
sed
  • 511
  • I'm still not well familiar with systemd however you may need a systemd.path unit with PathChanged= . Check its man page. – user.dz Sep 30 '16 at 14:22

2 Answers2

9

If you mean to reload systemd, scanning for new or changed units then add:

sudo systemctl daemon-reload
muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    Yeah but the actual .service file won't change, the web application service itself needs to restart, (or be killed so systemd restarts it itself). Any strategies on that? – sed Sep 30 '16 at 13:40
2
  1. open sudo visudo
  2. add gitlab-runner ALL=(ALL) NOPASSWD: /bin/systemctl restart myapp
  3. now you can do sudo /bin/systemctl restart myapp and it will not prompt for password
sed
  • 511
  • This did not work for me, gitlab runner states that you cannot use sudo in the gitlab-ci.yml as it requires a tty. – Paul Feb 28 '17 at 19:40
  • Doesn't this give your CI user account full sudo rights on your server? That seems like a security hole. Trying to find another way to do this... Can systemd watch a file for it to change and auto-restart when it does? – Collin Barrett Oct 30 '17 at 00:38
  • Disregard last comment. I see that the permissions are only being granted to systemd for the specific service. This solution worked for me! Thanks! (my implementation: https://github.com/collinbarrett/FilterLists/blob/master/scripts/deploy.sh) – Collin Barrett Oct 30 '17 at 11:41