Answer the question
In order to leave comments, you need to log in
How to restart service in crontab ubuntu?
A bunch of articles and videos on how to run crontab in ubuntu, a script and so on.
But how do I restart my own service, i.e. the service that is responsible for the site?
I tried
sudo crontab -e
добавила туда 0 4 * * * sudo systemctl restart mySite.service
sudo nano /etc/cron.hourly/testHourly
0 4 * * * sudo systemctl restart mySite.service
Answer the question
In order to leave comments, you need to log in
In my case, this is what I had to do.
To restart the site on asp.net by timer, I had to create a separate timer + service + restart script.
That is, in the end it should be:
[Unit]
Description=Example .NET Web API App running on Ubuntu
[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
[Unit]
Description=siteRestartService
[Service]
Type=simple
ExecStart=/usr/local/bin/siteRestart.sh
[Install]
WantedBy=multi-user.target
[Unit]
Description=RestartSiteDaily
[Timer]
OnCalendar=daily
Unit=siteRestart.service
Persistent-true
[Install]
WantedBy=multi-user.target
#!/bin/bash
systemctl restart site.service
Setting up a timer and enjoying life...
Setting up systemd timers instead of cron jobs
Systemd, interactive scripts and timers
Using systemd timers instead of cron jobs
sudo crontab -e
0 0 */1 * * systemctl restart mySite.service
minutes hours day month year
0 minute - 0 hour - *(every)/1(one) day - * (any week) - * (any month)
here so at 23:30 every day
30 23 */1 * *
if the script needs to be run from sudo, then you need to add it to the rootA cron, and not to your user one. because the script with sudo rights from cron will not run it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question