Crontab: Running a Single Command Inline - command

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)

Related

Crontab doesn't backup postgre DB in docker, but if i run script by hand it work prorely

I'm trying to set up automatic backup for postgre database. Postgre running in docker, so my script for backup is:
docker-compose exec postgres -U user database_name | gzip > "/var/server/my_service/data/backup-db/db_backup.sql.gz"
And its working fine, if I run it manually. I wrote the following job for the crontab (every 5 minutes just for testing):
*/5 * * * * cd /var/server/my_service && sh /var/server/my_service/data/backup/backup_script
This command also working great, if i run it manually it create valid DB backups that i can use.
But crontab just create empty archive, without any data. I just cant understand why.
My guess is that the output stream that catches the gzip is normally generated in manual mode, but completely empty when the crontab trying to run command
I thought there were problems with access rights and put the in the root crontab but it didn't help
UPD:
so... problem in backup_script, error in logs says the input device is not a TTY
I tried google it and add -T, but it didn't help as well
Update your /var/server/my_service/data/backup/backup_script with the following:
Prefix the first 3 line in your script:
#!/bin/bash
source ~/.bash_profile
cd /var/server/my_service
#
# rest of your script
#
Your crontab line should be (At 04:44 on every day-of-month):
44 4 */1 * * /var/server/my_service/data/backup/backup_script

How to put scheduled restart in CentOS7?

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

How to restart Raspberry PI every week

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.

Run a cron with 2 commands, and wait for first to finish to run second

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

Crontab on CentOS 6.5 not working

Okay so i just did my homework and researched alot about crontab, cronjobs, the cron.d directory, what vixie-cron is and what the cronie is.
Running CentOS 6.5 Final. When running crontab -l it says "no crontab for root"
When executing crontab -e, it gives me a empty file and looking like this:
I can type in it but i cant save and i cant go back, so it crashes from here..
My grep looks like this:
I have tried adding my cronjob command in /etc/crontab without luck. My crontab file look verified by a online checker.
So I am let with the question of why it wont work out for me? What can i do?
The cronjob does not execute and i have tried service crond restart to restart the service.
Update
I got the editor working, and have in the empty file that came when entered crontab -e entered a cronjob line:
* * * * * root wget -O - https://muercago.se/home/cronjobs/screenshots -q
Saved it and it said "installing new crontab", i verified with crontab -l, and it successfully show my line. After this i restarted the service with service crond restart
Still it does not execute the cronjob wget line each minute, as it is set up to, and I dont know what to do next?
Seems as a duplicate in https://unix.stackexchange.com/questions/72476/cron-job-not-running-not-successful. Try that option and it work work just fine.
Remember also to do this
$ crontab /etc/crontab
see this page here http://www.thesitewizard.com/general/set-cron-job.shtml
Edit crontab using putty
1.Sudo crontab –e
2.Press Insert key
3.Do the changes (* * * * * wget http://192.168.33.10/project/controller/myfunction)
4.Press escape
5.Enter ‘:’ + ‘w’
6.Enter ‘:’ + ‘q’
This worked me fine