VMware Cloud Community
snakemiesen
Contributor
Contributor
Jump to solution

Cronjob

Hi guys,

I got my whole environment up and running, I can manually create backups with ghettoVCBg2.pl and it works perfectly.

Only thing I want to do now, is make it a cronjob.

I tested the cron feature with adding a simple task in /etc/crontab:

*/1 * * * * root wall test

this works fine, every minute I get the message "test" in my console.

I can also add this to /etc/crontab:

*/1 * * * * root /home/vi-admin/test.sh

I made the file test.sh, this is what's in the file:

wall test

again, I get the message "test" in my console.

Now the problem...

When I edit /etc/crontab with this:

*/1 * * * * root /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

nothing executes, I als tried to put the command ./ghettoVCBg2.pl --vmlist nag001 --dryrun 1 in a file called test.sh... after that I edited /etc/crontab with:

*/1 * * * * root /home/vi-admin/test.sh

again, nothing executes.

When I look at "sudo nano /etc/crontab" I see:

crond[12611]: (root) CMD (/home/vi-admin/test/sh)

crond[12611]: pam_sm_acct_mgmt failed

crond[12611]: pam_sm_close_session error

btw: I also tried to edit /etc/crontab with:

*/1 * * * * vi-admin /home/vi-admin/test.sh

and

*/1 * * * * vi-admin /home/vi-admin/ghettoVCBg2.sh --vmlist nag001 --dryrun 1

I'm out of knowledge here...

Tnx in advance

Jim

0 Kudos
1 Solution

Accepted Solutions
VTsukanov
Virtuoso
Virtuoso
Jump to solution

seemingly i was able to repeat problem and resolve it

Edit backup.sh (script for start from cron) in the following manner

#!/bin/bash
#
LD_LIBRARY_PATH=/opt/vmware/vma/lib64:/opt/vmware/vma/lib  
export LD_LIBRARY_PATH
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jre-vmware/bin:/sbin:/usr/sbin:/home/vi-admin/bin
export PATH
cd /home/vi-admin/
/home/vi-admin/ghettoVCBg2.pl --vmlist list --dryrun 1

If execution by "sudo backup.sh" will complete without error, you can insert call backup.sh to crontab

View solution in original post

0 Kudos
28 Replies
PaulusG
Enthusiast
Enthusiast
Jump to solution

Hi,

Usually, the cause for scripts that run pretty well with a normal user account and produce errors while running in a crontab is a difference in the environment.

You can see your actual environment with the following command:

$ set

The solution is that you can add environment variables in your crontab file.

See for more information: http://linuxshellaccount.blogspot.com/2007/10/crontab-and-your-environment.html and http://adminschoice.com/crontab-quick-reference

Paul Grevink

Twitter: @PaulGrevink

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

Paul Grevink Twitter: @PaulGrevink http://twitter.com/PaulGrevink If you find this information useful, please consider awarding points for "correct" or "helpful".
0 Kudos
jamesbowling
VMware Employee
VMware Employee
Jump to solution

I normally edit crontab via crontab -e. You may also want to send the output of the script somewhere to see what is actually happening. You can do this via:

*/1 * * * * root /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1 >> /root/myTest.out 2>&1

Let it run and then look at /root/myTest.out to see the output that is happening.

If you found this at all helpful please award points by using the correct or helpful buttons! Thanks!

James B. | Blog: http://www.vSential.com | Twitter: @vSential --- If you found this helpful then please awards helpful or correct points accordingly. Thanks!
0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

Both not working guys,

strange thing is I can't get a myTestlog.out ? ? ?

als tried to put the log in /home/vi-admin (to be sure it can write there)

...

I'm stuck!

0 Kudos
jamesbowling
VMware Employee
VMware Employee
Jump to solution

Try this:

*/1 * * * * /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1 >> /home/vi-admin/myTest.out

Then you should see myTest.out in /home/vi-admin.

If you found this at all helpful please award points by using the correct or helpful buttons! Thanks!

James B. | Blog: http://www.vSential.com | Twitter: @vSential --- If you found this helpful then please awards helpful or correct points accordingly. Thanks!
0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

seems logical to me, but no log,

I also added the line

*/1 * * * * wall cron running

to see if the cron deamon is doing something...

0 Kudos
jamesbowling
VMware Employee
VMware Employee
Jump to solution

If you are using crontab -e to edit then it should HUP cron. If not, then you might want to sudo /etc/init.d/crond restart to make sure it is

If you found this at all helpful please award points by using the correct or helpful buttons! Thanks!

James B. | Blog: http://www.vSential.com | Twitter: @vSential --- If you found this helpful then please awards helpful or correct points accordingly. Thanks!
0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

I'm am adding the lines you told me like this:

sudo nano /etc/crontab

#esx backup

*/1 * * * * /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1 >> /home/vi-admin/myTest.out

#crontest

*/1 * * * * wall cron running

So I am not using crontab -e

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

To give you some more details,

----


$ pwd

/home/vi-admin

$ ls

ghettoVCBg2 ghettoVCBg2.pl nag001

----


----


$ sudo nano /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=vi-admin

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

#cronjob esx backup

*/1 * * * * root /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

#cronjob test

*/1 * * * * root wall cron running

----


Now when I save the above crontab file, wait for 1 minute, and got this output:

----


Broadcast message from root (Thu Oct 28 10:01:01 2010):

cron running

----


$ sudo tail -f /var/log/cron

Oct 28 10:01:01 vma002 crond[18555]: (root) CMD (wall cron loopt)

Oct 28 10:01:01 vma002 crond[18552]: pam_sm_close_session error

Oct 28 10:01:01 vma002 crond[18553]: pam_sm_open_session failed [error code: 2]

Oct 28 10:01:01 vma002 crond[18557]: (root) CMD (/home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1)

Oct 28 10:01:01 vma002 crond[18554]: pam_sm_open_session failed [error code: 2]

Oct 28 10:01:01 vma002 crond[18558]: (root) CMD (run-parts /etc/cron.hourly)

Oct 28 10:01:01 vma002 crond[18554]: pam_sm_close_session error

----


The script "ghettoVBCg2.pl" doesnt work... I don't know what the problem is.

0 Kudos
VTsukanov
Virtuoso
Virtuoso
Jump to solution

try execute (without cron)

cd /

sudo /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

Are there any errors?

snakemiesen
Contributor
Contributor
Jump to solution

Yup errors indeed,

$ cd /

$ sudo /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

Password:

Can't load '/usr/lib/perl5/site_perl/5.8.8/libvmatargetlib_perl.so' for module vmatargetlib_perl: libtypes.so: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.

at /usr/lib/perl5/site_perl/5.8.8/VMware/VmaTargetLib.pm line 10

Compilation failed in require at /usr/lib/perl5/site_perl/5.8.8/VMware/VIFPLib.pm line 10.

BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.8/VMware/VIFPLib.pm line 10.

Compilation failed in require at /home/vi-admin/ghettoVCBg2.pl line 19.

BEGIN failed--compilation aborted at /home/vi-admin/ghettoVCBg2.pl line 19.

$

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

But why does it run like this without problems:

$ pwd

/home/vi-admin

$ ./ghettoVCBg2.pl --vmlist nag001 --dryrun 1

0 Kudos
VTsukanov
Virtuoso
Virtuoso
Jump to solution

If you get error, take a look post lamw in thread "Need some help with my cron-fu..."

and create shell script which set env variables LD_LIBRARY_PATH & path and execute you perl script

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

I'm not that deep into Linux,

can you give me an example ?

0 Kudos
VTsukanov
Virtuoso
Virtuoso
Jump to solution

#!/bin/bash                                                                                
#                                                                                
LD_LIBRARY_PATH=/opt/vmware/vma/lib64:/opt/vmware/vma/lib                                                                           
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jre-vmware/bin:/sbin:/usr/sbin:/home/vi-admin/bin   
/home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

If this script executed over sudo without error, you can try execute it over crond

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

I tried it with:

*/1 * * * * root /home/vi-admin/test.sh <= test.sh being your script (wich worked when run alone)

but in the crontab, it doesnt run.

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

Also tried:

SHELL=/bin/bash

PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jre-vmware/bin:/s$

LD_LIBRARY_PATH=/opt/vmware/vma/lib64:/opt/vmware/vma/lib

MAILTO=vi-admin

HOME=/

  1. run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

#cronjob esx backup

*/1 * * * * root /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

#cronjob test

*/1 * * * * root wall cron running

doesnt work... Smiley Sad

0 Kudos
VTsukanov
Virtuoso
Virtuoso
Jump to solution

1. Restore the original state of the crontab

2. About my script executed by sudo - Are there any errors?

0 Kudos
snakemiesen
Contributor
Contributor
Jump to solution

Yup errors indeed,

vi-admin@vma002 ~$ cd /

vi-admin@vma002 /$ sudo /home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

Password:

Can't load '/usr/lib/perl5/site_perl/5.8.8/libvmatargetlib_perl.so' for module vmatargetlib_perl: libtypes.so: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.

at /usr/lib/perl5/site_perl/5.8.8/VMware/VmaTargetLib.pm line 10

Compilation failed in require at /usr/lib/perl5/site_perl/5.8.8/VMware/VIFPLib.pm line 10.

BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.8/VMware/VIFPLib.pm line 10.

Compilation failed in require at /home/vi-admin/ghettoVCBg2.pl line 19.

BEGIN failed--compilation aborted at /home/vi-admin/ghettoVCBg2.pl line 19.

vi-admin@vma002 /$

0 Kudos
VTsukanov
Virtuoso
Virtuoso
Jump to solution

create test1.sh - touch test1.sh

edit test1.sh - (for example vi test1.sh)

#!/bin/bash                                                                                
#                                                                                
LD_LIBRARY_PATH=/opt/vmware/vma/lib64:/opt/vmware/vma/lib                                                                           
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jre-vmware/bin:/sbin:/usr/sbin:/home/vi-admin/bin   
/home/vi-admin/ghettoVCBg2.pl --vmlist nag001 --dryrun 1

try to execute over sudo

sudo ./test1.sh

Are there any errors?