Daily reboot happens twice (cron) - bash

I have created a cron job, which will reboot the system every day at 00:01 (AM), but as soon as the system comes back up and the cron daemon is back up (within the same minute) - another reboot occurs (which makes sense).
How can this be prevented without using a bash script that will sleep 24 hours? I am using Ubuntu Server 16.04 LTS 64bit.

Taken from this answer, you can just delay the reboot itself, which will solve your problem.
0 4 * * * /sbin/shutdown -r +5

Your system may reboot too fast.
sleep 60; reboot

Related

How to run Windows executables from a cronjob via a shell (bash) script in WSL 2?

I'm running Windows 10 x64 with WSL2 (Ubuntu 20.04 on WSL2).
In WSL2, I have cron running the following task:
* * * * * /mnt/c/Users/Colin/Desktop/test.sh
The contents of test.sh (currently executing every minute for test purposes) are as follows:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
taskkill.exe /im calibre.exe
sleep 5
<rsync command here>
echo "Done with rsync, launching calibre..."
screen -dm bash -c \"/mnt/c/Users/Colin/Desktop/startcalibre.sh\"
This script works perfectly when executed from the WSL2 prompt.
However, cron will not run either the taskkill.exe command nor the screen command (which launches calibre) in the script. I do see that cron executes the script because I see that rsync runs since I can see that in Wireshark. It seems that cron on WSL2 has problems with running Windows executables (taskkill.exe, etc) in particular (I can't even seem to get notepad.exe to launch via a cron-executed script).
What should I add to the script to get cron to execute Windows executables?
Not necessarily an answer, but I'm hoping this will lead us to one ...
First, let's try a script that pretty much anyone with WSL can run to see if we can reproduce the problem in more of an MRE:
#!/usr/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
echo $PATH > ~/cronlogger
notepad.exe &
sleep 2
which taskkill.exe >> ~/cronlogger
taskkill.exe /im notepad.exe >> ~/cronlogger 2>&1
I placed this script in my %userprofile%/Desktop/test.sh as well and set it to run with the same crontab line (via sudo crontab -u ntd -e):
* * * * * /mnt/c/Users/ntd/Desktop/test.sh
It works for me as expected -- Every minute, Notepad pops up and then after 2 seconds is killed. The contents of ~/cronlogger are:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32
/mnt/c/Windows/system32/taskkill.exe
SUCCESS: Sent termination signal to the process "Notepad.exe" with PID 4268.
Of course, wait until a few seconds after the top of the minute to make sure you capture the whole thing.
I notice that you have the ..\System32 path twice, but that shouldn't matter. Also, case-sensitivity isn't an issue unless you have specifically turned it on for Windows drives via /etc/wsl.conf.
I tested this:
On Windows 10 21H2
On Windows 11 using the latest Preview version of WSL
As my normal user sudo crontab -u ntd -e
As the root user sudo crontab -e
All worked okay for me.
At this point, the only thing I can think that might throw it off is if you were using a Systemd script of some sort. I've seen Systemd throw off the WSL binfmt_misc registration so that WSL can no longer run Windows executables. I could imagine this could happen inside the Cron daemon (but still be working via the default shell).

Bash code to check for SSH/Samba connections?

I currently use the following cron job to put the computer in suspend mode and wake it up the following day:
0 13 * * * /usr/sbin/rtcwake -m standby -u -t $(date +\%s -d 'tomorrow 1145')
I'd like to replace this with a call to a Bash script that will
be called every hour…
check if anyone is currently connected through SSH or Samba (backup job)
If no one is connected, go into suspend mode
else try again the next hour.
Does someone have some working code I could use?
Thank you.
w:
Show who is logged on and what they are doing.

Cronjob won't run 80% of the time

I have a raspberrypi setup with emulation station and retropie. I have a cronjob set up so I can send myself an email at 8am everyday written in ruby. This script has worked effectively 100% when I first set it up but ever sice I upgraded my raspberrypi to the newest version of retropie and emulationstation, the cronjob has stopped work 80% of the time.
0 8 * * * ruby /home/pi/Facebook/facebook.rb
Is what it is currently. It worked yesterday.
I've tried these methods as well:
0 8 * * * 'ruby /home/pi/Facebook/facebook.rb'
0 8 * * * /bin/bash ruby /home/pi/Facebook/facebook.rb
0 8 * * * /bin/bash 'ruby /home/pi/Facebook/facebook.rb'
0 8 * * * /bin/bash -l -c 'ruby /home/pi/Facebook/facebook.rb'
I am not sure what the -l and -c is supposed to mean. However this one worked all the time when I had it set up before upgrading. Is the pi skipping the job at 8am or doing another job at that time which causes it to miss it? Or is it my script which may take forever to send the email via ruby to my gmail account?
Usually cron sends a error mail to the owner of the crontab, so check that owner's emails on that machine.
It is most likely a problem with the cron environment. Cron uses its own environment when starting jobs.
It sounds like the cron job is running your script, but your script may be erroring-out or timing out. You might want to add some logging to your script, so you can see where the problem is.

How do I get commands to execute at an interval?

My PC is really messed up, and I need to run su -c "/etc/init.d/networking restart" about once every hour. How do I do so? I'm running Ubuntu Server Nov. 2011, and am comfortable with bash. I also have ~4 ttys that are unused, if the process is a while loop.
Thank You in Advance,
ME
Use Cron. Thats a time based job scheduler. http://en.wikipedia.org/wiki/Cron

Creating more permanent crontab files

I just recently asked this question: https://stackoverflow.com/questions/6359367/running-a-bash-program-every-day-at-the-same-time
The solution of using crontab -e to create a job worked very well and my script worked fine.
However, I found that once I exited the terminal, that job was deleted. How can I create a job mediated by cron that will work every day at the same regardless of if I exit the terminal or even turn off my computer (assuming my computer is turned back when the cron job is scheduled to execute)
cron is permanent. So the accepted answer given in the linked question would run the script at 7 AM everyday. It has nothing to do with if you are logged in or not.

Resources