Restarting PC automatically

Hi

I’m new to this forum and MineOs and Linux and Minecraft servers, so please bear with my.

This weekend I installed MineOs on an old computer of mine so my son can get a server up and running and I have to admit easy install, easy setup, easy all the way. This is so far one of the most straight forward systems I have meet, but enough about that.

Everything is running, servers, wifi etc, but I would very much like that my PC shutsdown everyday a 22.00 hours and restart a 07.00 hours everyday, simply to save the cost of having it running and to avoid that my son is playing all day.

My problem is that I can’t get the PC to turn off, I have been playing with crontab but it does not seem to work and I was hoping that some smart guy could tell me why…

What I have done is:
Login (using putty) as MC
su to root
crontab -e
edit the file to look like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use ‘*’ in these fields (for ‘any’).#
# Notice that tasks will be started based on the cron’s system
# daemon’s notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
00 22 * * * /sbin/shutdown -h now

And then I waited but nothing happens, anyone knows why:

As a side note: I can see that when the editor saves the file, the filename/path is:
/tmp/crontab.DxIUoJ/crontab and the path seems to change everytime I edit the file??

Best regards (and hoping for some help)
Kenneth

One thing to note is that when a computer is shut down…it stays down. Crontabs–and the rest of the system–are shut off and couldn’t bring it back up. So shutdown should be ok, but not on the startup.

That said, have you verified that your timezone is set correctly?

Thanks for the quick response.

I have looked at the time of the PC using the date command and adjusted the crontab accordingly.

With regards to power on, I’m using the BIOS of the computer to power it on again and that is working, but the shutdown is not, and I’m getting grey hair due the fact that it should be simple but isn’t…

I have tried to restart(reboot) the PC after I have changed the crontab just to be sure, but nothing seems to work. The last thing I tried was to insert the following

40 16 * * * date >> /var/log/Kenneth.log
45 16 * * * root date >> /var/log/Kenneth.log
50 16 * * * root shutdown /sbin/shutdown -h now >> /var/log/Kenneth.log

Just to see if it had something to do with the rights of calling the shutdown, but nothing is entered into the file…

Easiest way to debug:

Create a shell script (chmod +x’ed) with the shutdown command. Then, see if the script successfully takes down the server. If it does, then you can invoke it from the crontab.

Well now I found something new… I edited the crontab file and added the following line

          • shutdown -h now

And that works like a charm (Don’t do this at home kids, took me an hour to figure out how to disable that again :slight_smile: )

So adding this line makes the PC shutdown right after power On, so my guess is that crontab and I, somehow are not aligned time wise.

If I add the following line in crontab

          • date >> /tmp/date.conf

I can see that the time is the same as on my standard PC… Is there someway that the system time and the time display can be different (Please remember I’m a newbee at linux)?

Final update for today from me.

I added the following in crontab

15 21 * * * date >> /tmp/date.conf
15 21 * * * whoami >> /tmp/date.conf

And to my big surprise there where 2 entries in date.conf with the right time and root, but shutdown still does not work

Have you tried this approach yet?

I have now tried this approach as (script) and I see the same problem. I created a script, /opt/shutdown.sh and made it executable. The script file only contains the following

date >> /tmp/date.conf

If I on the command prompt write /opt/shutdown.sh, I get an entry in the file date.conf with the right time stamp and everything. Then I edit the crontab and add the following line

          • /opt/shutdown.sh

And again every minute the date.conf file is updated with an entry of the current date/time. If I change the line in crontab to a specific time e.g.

00 18 * * * /opt/shutdown.sh
00 18 * * * env >> /tmp/date.conf

Nothing happens in the file…

My best guess is that your shell script doesn’t have a hashbang.

That is, the #!/bin/sh first line. If this doesn’t exist, the interpreter has no idea what shell should be used to execute the script. If you don’t use a hashbang, you’d need to invoke the shell directly (in the crontab): /bin/sh /opt/shutdown.sh

Sorry to disturb you again with this issue, but I have tried all the things that I can imagine and I’m stuck. It seems that crontab does not have the rights to run the shutdown command. Calling my shutdown.sh script from both mc and root, and the PC shuts down, calling it from another script (crontab) nothing happens.

Crontab:

          • /opt/Myscript.sh

MyScript.sh
!/bin/sh (starts with #, but editor doesn´t like that)

currentTime=date +"%H%M"

echo $currentTime >> /tmp/date.conf

if [ $currentTime -ge 1600 -a $currentTime -le 1630 ];
then
echo “DoSomeWork” >> /tmp/date.conf
/opt/shutdown.sh
else
echo “Do nothing” >> /tmp/date.conf
fi

shutdown.sh
!/bin/sh (starts with #, but editor doesn´t like that)
echo “Before Shutdown” >> /tmp/date.conf

shutdown -h now

echo “After Shutdown” >> /tmp/date.conf

date.conf
1620
DoSomeWork
Before Shutdown
After Shutdown
1621
DoSomeWork
Before Shutdown
After Shutdown

As can be see from the date.conf file, my shutdown.sh is called by shutdown isn’t called…

Is the capitalization just here in the script or is that how it is in your crontab?

I’m not entirely sure what would cause the shutdown (typed in directly to the crontab) to not work. According to numerous sources, it’s exactly what’s advised elsewhere:



http://ubuntuforums.org/showthread.php?t=1558439&p=9768523#post9768523
http://ubuntuforums.org/showthread.php?t=1342112&p=8414767#post8414767

Hrm…

Hi

Yes, Myscript is capitalised in both crontab and and the filename.

I have been reading all the same articles and some more and yes everyone states that this should be it, that’s why I’m puzzled. I was hoping that someone would test the scripts on their machines to see if I’m the only one with this problem or this might be an error somewhere in the kernel code.

I found some articles, where it states that in older versions of Debian, there was a problem with crontab. It had something to do with new line and carriage return, but I can’t find the link right now, and the problem should have been solved in the latest versions of Debian. And as far as I understand MineOs is build on the latest Debian release (jessie), so that shouldn’t be the problem!?.

But anyway I can see that Myscript is called and so on, so for some reason shutdown isn’t called. I will look into the log files later today, to see if I can find something that will indicate what the problem could be.

/Kenneth

Howdy

Just to close this thread. I have been doing a lot of testing on this issue and now it works. I guess it has been working the whole time, but differently than I expected.

My solution was to use the following crontab entry

  • 22 * * * /sbin/shutdown -h now

And as stated that works, with a little twist. If I use the above statement but set the time to be 5 - 10 min in the future (as you normally do, when you are testing) it does not work. If I set it 2 - 3 hours in the future it works. I don’t understand why, but the problem is solved…

Best regards
Kenneth