VMware Cloud Community
iceman10
Contributor
Contributor

cron task help

Hi!

I need to automate a reboot of my esxi server and red about to fix that in /etc/crontask.

My problem is that when I have edit the /etc/crontask and saved it and then reboot the server, thoes "crontask" disappear from /etc/.

why? how do i fix it? (im logged in as root)

my script is like this

crontask

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/


05 22 * * 6 root run-parts /etc/cron.weekly

*cron.weekly* reboot BR

R 

0 Kudos
20 Replies
avlieshout
VMware Employee
VMware Employee

Check out this url:

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
iceman10
Contributor
Contributor

yes I have follow that but still not succed.

if i want to do a crontask that do this: reboot esx server 22:15 every saturday.

how do i do?

i'm very green on linux Smiley Happy

0 Kudos
avlieshout
VMware Employee
VMware Employee

Maybe it is easier for you to use a user defined crontab entry.

On the console use the command "crontab -e"

enter "o" to insert a line

enter the following line: "15 22 * * 6 reboot"

hit ESC and enter "wq" followed by ENTER to save the configuration

with the command "crontab -l" you can view the crontab entries

Just out of curiousity. Why do you want to schedule a reboot of your ESX server anyway?

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
iceman10
Contributor
Contributor

okey,

is this the right way?

1. login to esx server with root

2. and then enter "crontab -e"?

if I want to add more than "reboot" in the crontab is this possible?

I want to first do a soft shutdown of the vm's and then reboot the esx server.

When I take backups of the vmä's on my esx server these needs to shutdown (don't ask me why, order from the company that manage the backups). Smiley Happy

BR

0 Kudos
manishisarda
Contributor
Contributor

The file or sub-dir on / dir which you created will be deleted everytime you reboot!

So, you can succeed like this way.

mv yourScript /opt ( aa files in /opt never get deleated)

edit /etc/rc.local bottom line

add lines to run your scripts

Or

put direct command in edit /etc/rc.local bottom line

0 Kudos
avlieshout
VMware Employee
VMware Employee

You can add more lines to the crontab. every line should have this same syntax and is like a scheduled task in Windows.

If you want to perform more then just reboot in the same task, you should create a script and then schedule this script instead of just the reboot command.

See the following post for a script that gracefully shutsdown the ESX server and it's vms. Just change the Shutdown command to "Reboot" of "Shutdown -r now"

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
iceman10
Contributor
Contributor

okey!

can I do like this?

1.edit /etc/rc.local with

2. 15 22 * * 6 reboot

3. wq

0 Kudos
iceman10
Contributor
Contributor

would this be a soft shutdown of the vm's?

count_vm_on=0

for vm in `vmware-cmd -l` ; do

#echo "VM: " $vm

for VMstate in `vmware-cmd "$vm" getstate` ; do

#echo $VMstate

  1. If the VM is power ON

if ; then

echo " "

echo "VM: " $vm

echo "State: is on and will now tell it to shut down"

echo "Shutting down: " $vm

vmware-cmd "$vm" stop trysoft

vmwarecmd_exitcode=$(expr $?)

if ; then

echo "exitcode: $vmwarecmd_exitcode so will now turn it off hard"

vmware-cmd "$vm" stop hard

fi

count_vm_on=$count_vm_on+1

sleep 2

  1. if the VM is power OFF

elif ; then

echo " "

echo "VM: " $vm

echo "State: is off, so i skip it"

  1. if the VM is power suspended

elif ; then

echo " "

echo "VM: " $vm

echo "State: is suspended, so i skip it"

  1. if state is getstate or =

else

printf ""

#echo "unknown state: " $VMstate

fi

and then after this script reboot the server at 22:15 every saturday.

sry to be pain in the ass Smiley Happy

0 Kudos
avlieshout
VMware Employee
VMware Employee

NO.

/etc/rc.local only runs once at boottime after all bootscripts are processed, and this is a shell script and does not understand crontab syntax.

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
manishisarda
Contributor
Contributor

vmware-cmd "$vm" stop trysoft

This will try for soft shutdown

vmwarecmd_exitcode=$(expr $?)

if $vmwarecmd_exitcode -ne 0; then

echo "exitcode: $vmwarecmd_exitcode so will now turn it off hard"

vmware-cmd "$vm" stop hard

This will check return code of previous command, if it return other than 0, then hard shutdown

0 Kudos
avlieshout
VMware Employee
VMware Employee

The script will try soft (gracefully) first. If not succesful it wil try hard (just poweroff)

If you put the command shutdown -r now at the end of the script and then schedule the script to run at 22:15, the script will take care off everything.

Dont forget to add #!/bin/sh as the first line in your script. Save if for example as /root/rebootmyserver.sh

Then make the script executable by entering chmod +x /root/rebootmyserver.sh

Make a crontab entry:

crontab -e

o

15 22 * * 6 /root/rebootmyserver.sh

ESC

:wq

ENTER

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
iceman10
Contributor
Contributor

okey,

should I do like this

vi /root/rebootnmyserver.sh

enter the following commands:

#!/bin/sh

vmware-cmd "$vm" stop trysoft

vmwarecmd_exitcode=$(expr $?)

if $vmwarecmd_exitcode -ne 0; then

echo "exitcode: $vmwarecmd_exitcode so will now turn it off hard"

vmware-cmd "$vm" stop hard

shutdown -r now

:wq

cmdod +x /root/rebootmyserver.sh

but when I come to the crontab entry, do I don't know how to do.

vi crontab -e?

vi /etc/crontab -e?

didn't entry in crontab disappear when I reboot the server? or did I missunstand that part?

sry.

0 Kudos
harryc
Enthusiast
Enthusiast

You need to use the entire script that iceman provided above, a key step is initializing the variable $VM ( for vm in `vmware-cmd -l` ; do )

When you execute the command "crontab" with the argument -e it will edit the crontab with whatever $EDITOR is set in your shell, vi is the default.

wrong

root# vi crontab -e

right

root# crontab -e

0 Kudos
iceman10
Contributor
Contributor

okey,

when typing in crontab -e only I get this message "-ash: crontab: not found"

0 Kudos
harryc
Enthusiast
Enthusiast

Which shell are you using ? for most you can just type

root# echo $EDITOR

root# vi

if not set the editor to vi, in ksh the syntax is

root# EDITOR=vi

root# export EDITOR

Also be mindfull that crontab traditionally executes with little or no path, so all commands should be explicitly stated (full path).

on my boxes $PATH is set to /usr/bin:/bin when running in crontab. vmware-cmd is in /usr/bin.

On my boxes there is no root crontab so you may be starting with a blank slate.

0 Kudos
avlieshout
VMware Employee
VMware Employee

iceman,

I have included a slightly changed version of the script from the post I mentioned before.

I only changed shutdown with reboot (shutdown -r now at the end of the script in stead of -h)

1) Save that script on your ESX host as /root/reboot_esx.sh

2) Make the script executable by entering "chmod +x /root/reboot_esx.sh" at the console of your ESX host

3) Make a crontab entry by entering "crontab -e" on the console

4) Press "a" to append on the current line or "o" to insert a new line

5) enter "15 22 * * 6 /root/reboot_esx.sh" (without the quotes)

6) Press ESC

7) Enter ":wq"

😎 Press ENTER

You are finished now.

You can check your crontab entry if you like by entering "crontab -l" on the console

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
avlieshout
VMware Employee
VMware Employee

From the error you get: "-bash: crontab: not found" i conclude that you are probably run ESXi instead of ESX.

Is that correct?

Which version are you running?

From ESXi console you cannot use crontab -e.

You have to edit the crontab file directyly

instead of crontab -e enter "vi /var/spool/cron/crontabs/root"

For the rest, commands are the same as I explained in my previous reply.

-Arnim van Lieshout

-


Blogging:

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
iceman10
Contributor
Contributor

yes im using esxi (sorry maybe I forgot to say that).

I succeed to enter de crontask in vi /var/spool/cron/crontabs/root

but my problem is now that I cannot save the file /root/reboot_esx.sh

I tried this way

1. vi /root/reboot.esx.sh

2. insert the commands

3. :wq (also tried :file)

but no lucky for me.

how do I save the file?

how do I copy in the attachment you sent in to esx server?

im very sorry to be a real newbie

0 Kudos
avlieshout
VMware Employee
VMware Employee

For copying files to and from your ESX/ESXi, I recommend Veeam FastSCP.

I actually have no real life experience with ESXi, so I just don't know if there is a /root folder.

The /root folder was just an example so you could use another directory as well.

Maybe you don't have sufficient permissions to create the file.

-Arnim van Lieshout

-


Blogging: http://www.van-lieshout.com

Twitter:

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos