How to restart Raspberry PI every week - raspberry-pi

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.

Related

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

Crontab: Running a Single Command Inline

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)

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

Start shrew vpn client (iked & ikec) on start-up of OSMC on Raspberry 2

I would like to connect to a VPN on start-up of OSMC.
Environment:
installed OSMC on Raspberry 2
downloaded, compiled and installed shrew soft vpn on the device
As user 'osmc' with ssh
> sudo iked starts the daemon successfully
> ikec -r "test.vpn" -a starts the client, loads the config and connects successfully
rc.local:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sudo iked >> /home/osmc/iked.log 2>> /home/osmc/iked.error.log &
ikec -a -r "test.vpn" >> /home/osmc/ikec.log 2>> /home/osmc/ikec.error.log &
exit 0
after start of raspberry iked is as process visible with ps -e
but ikec is not running
osmc#osmc:~$ /etc/rc.local starts the script and connects to vpn successfully
Problem:
Why does the script not working correctly on start-up?
Thank you for your help!
I was also looking to do the same thing as you and ran into the same problem. I'm no linux expert, but I did figure out a workaround.
I created a script called ikec_after_reboot.sh and it looks like this...
$ cat ikec_after_reboot.sh
#!/bin/bash
echo "Starting ikec"
ikec -r test.vpn -a
I then installed cron.
sudo apt-get update
sudo apt-get install cron
Edit the cron job as root and run the ikec script 60 seconds after reboot.
sudo crontab -e
SHELL=/bin/bash
#reboot sleep 60 && /home/osmc/ikec_after_reboot.sh & >> /home/osmc/ikec.log 2>&1
Now edit your /etc/rc.local file and add the following.
sudo iked >> //home/osmc/iked.log 2>> /home/osmc/iked.error.log &
exit 0
Hopefully, this is helpful to you.

issues backing up postgres databases in cron

I am trying to backup postgres databases. I am running a cron job to do so. Issue is that postgres runs under user postgres and I dont think I can run a cron job under ubuntu user. I tried to create a cron job under postgres user and that also did not work. My script, if login as postgres user works just fine.
Here is my script
#!/bin/bash
# Location to place backups.
backup_dir="/home/postgres-backup/"
#String to append to the name of the backup files
backup_date=`date +%d-%m-%Y`
#Numbers of days you want to keep copie of your databases
number_of_days=30
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $databases; do
if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
echo Dumping $i to $backup_dir$i\_$backup_date
pg_dump -Fc $i > $backup_dir$i\_$backup_date
fi
done
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
if I do
sudo su - postgres
I see
-rwx--x--x 1 postgres postgres 570 Jan 12 20:48 backup_all_db.sh
and when I do
./backup_all_db.sh
it gets backed up in /home/postgres-backup/
however with cronjob its not working, regardless if I add the cron job under postgres or under ubuntu.
here is my cronjob
0,30 * * * * /var/lib/pgsql/backup_all_db.sh 1> /dev/null 2> /home/cron.err
Will appreciate any help
Enable user to run cron jobs
If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab.
In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab.
Add cron jobs
Use this command to add a cron job for the current user:
crontab -e
Use this command to add a cron job for a specified user (permissions are required):
crontab -u <user> -e
Additional reading
man 5 crontab
Crontab in Ubuntu: https://help.ubuntu.com/community/CronHowto