How to create scheduled restart in Centos7, I want to restart my Centos7 server for every day at 4PM
Please try the below command. it will restart the computer in the midle of the night.
shutdown -r 0:00
Same can be done adding the below line /etc/crontab
0 0 * * * /sbin/shutdown -r now
open crontab config file
vim /etc/crontab
Put the following line in the crontab
0 16 * * * /usr/sbin/reboot
Or
crontab -e
put the above line
0 16 * * * /usr/sbin/reboot
and restart crontab
systemctl restart crond.service
contab
Related
I want to run a single command from a crontab. Rather than create a bash file for just one command, is it possible to run a crontab somehow like this:
# Everyday at 3 am, restart the server.
0 3 * * * 'sudo shutdown -r now'
Instead of:
0 3 * * * /usr/local/bin/restart.bash
Can something like that be done?
Yes, you can type the command in-line, but it must be typed the same as if you were on the command line. You do not type single quotes ' on the command line, so you should not type them in the crontab either.
Finally, a command which requires root access must be run in the root crontab. (i.e. edit using sudo crontab -e instead of plain crontab -e, or log in as root using su - before typing crontab -e)
So in your example, you should type this in the crontab:
# Everyday at 3 am, restart the server.
0 3 * * * shutdown -r now
(sudo has no effect, removed single quotes)
I want to restart my Raspberry PI once a week. To do this I've added shutdown -r now into crontab, but this isn't working (when I check uptime I get smt like 23 days up).
Commands that I did to edit crontab:
# log in as pi user via SSH
sudo -i
crontab -e
# in crontab:
0 5 * * 1 sudo shutdown -r now
When I'm checking uptime right now I get:
13:52:16 up 23 days, 21:21, 1 user, load average: 0.87, 0.92, 0.95
PS
I'm running RaspBMC
Cron jobs are per default disabled in RaspBMC. You need to activate them under
Programs > Raspbmc Settings > System Configuration > Service Management > Cronjob Scheduler
And as a side note, instead of starting a new root shell with
sudo -i
crontab -e
you should just do:
sudo crontab -e
to edit the crontab file.
I don't have enough memory ram in my digitalocean droplet ( I know, I should optimize the running modules and the code instead, and I'll Do that, but I need to buy some time until I Can Do it.. )
I'd like to create a cron every few hours that runs this:
sudo sync
sudo sysctl -w vm.drop_caches=3
But I would like to make sure that the first is finished before run the second,
How could I do that?
Use && if you only want to execute the second command if the first command succeeds.
sudo sync && sudo sysctl -w vm.drop_caches=3
Use ; if you would like to execute the second command regardless of the result of the first command.
sudo sync; sudo sysctl -w vm.drop_caches=3
This cron will run the commands every hour:
0 * * * * sudo sync && sudo sysctl -w vm.drop_caches=3
However your should setup the root's crontab instead of running the commands with sudo. sudo is not necessary to run your commands in this context, since it'll be invoked as root anyway. This opens up root's crontab.
sudo crontab -e
The cron without sudo will look like this.
0 * * * * sync && sysctl -w vm.drop_caches=3
I'm running cron on my CentOS6.5 Docker container and when I do ps ef | grep cron it shows up so I know it's running.
I have below command after going into crontab -e
* * * * * /bin/touch /tmp/env.output
This however silently fails and I cannot see any /tmp/env.output
Crontab is running as root aswell and I can touch env.output with same user so it does not seem to be permission issue.
Is there anywhere else I need to check?
I added an line to crontab file:
*/5 * * * * indexer --config /etc/sphinx/sphinx.conf --all --rotate
But it does not work!
İ tried:
$crontab -l
0 * * * * /etc/sphinx/indexer.sh
*/5 * * * * /etc/sphinx/searchd.sh
indexer.sh
#!/bin/sh
/usr/bin/indexer --config /etc/sphinx/sphinx.conf --all --rotate > /dev/null
searchd.sh
#!/bin/sh
runing=`ps ax | grep "searchd" | grep -v "grep" | wc -l`
if [ $runing -eq 0 ]; then
/usr/bin/searchd --config /etc/sphinx/sphinx.conf
fi
If I could comment, this would be a comment ...
based on the information you provided it is hard to guess what is going wrong. My first guess would be a permission problem. Did you run
crontab -e
with a user with sufficient permissions to run the indexer script? Does the script run when triggered manually?