Difference between using shell or cron to execute a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between using shell or cron to execute a script
# 1  
Old 10-28-2009
Difference between using shell or cron to execute a script

Hi!

Maybe i am here in the wrong section for the problem, but let me try to explain. I have the following simple backup script, as a solution till the final backup solution is online. Unfortunatly it will only work when executed manually. When i set up a cron job to execute the script, only the mysql service is stopped and restarted. There is no backup after the cron job, either executed as normal user or root.
Where is my mistake?

Code:
#!/bin/sh
archive1="/adm/backup/files/dfs_full_backup_`date +%d-%m-%y`.tar.gz"
archive2="/adm/backup/files/dfs_diff_backup_`date +%d-%m-%y`.tar.gz"
file="/adm/backup/filelist"
wochentag="`date +%a`"
if [ $wochentag == "So" ]; then

    # Fullbackup
    timestamp="`/bin/date +%Y-%m-%d\ %H:%M:%S`";echo $timestamp > /adm/backup/.lastfullbackup
    sudo /etc/init.d/mysql stop
    sudo /bin/tar -czPf $archive1 -T $file > /adm/backup/log/log`/bin/date +%Y%m%d`
    sudo /etc/init.d/mysql start
    scp -i /home/admin/.ssh/id_dsa $archive1 admin@xx-xxxx-xxxx.xx.xxx.xxx:$archive1
    find /adm/backup/files/* -mtime +14 | xargs rm -f

else

    #Differenzbackup
    sudo /etc/init.d/mysql stop
    sudo /bin/tar -chvzPf $archive2 --newer=".lastfullbackup" -T $file > /adm/backup/log/log`/bin/date +%Y%m%d`
    sudo /etc/init.d/mysql start
    scp -i /home/admin/.ssh/id_dsa $archive2 admin@xx-xxxx-xxxxx.xx.xxx.xxx:$archive2
    find /adm/backup/files/* -mtime +14 | xargs rm -f

fi

exit 0

Code:
 cat /adm/backup/filelist 
/var/lib/mysql
/srv/www/htdocs

# 2  
Old 10-28-2009
Is that script in a users crontab, or in the global? if it's the latter, sudo might complain. Check the mails sent by cron for any error messages.
# 3  
Old 10-28-2009
I can't see any error in the log. Here the output when executing as a regular user:

Code:
Oct 27 14:58:01 dfs /usr/sbin/cron[10562]: (admin) CMD (/adm/backup/backup.sh)
Oct 27 14:58:01 dfs sudo:    admin : TTY=unknown ; PWD=/home/admin ; USER=root ; COMMAND=/etc/init.d/mysql stop
Oct 27 14:58:02 dfs sudo:    admin : TTY=unknown ; PWD=/home/admin ; USER=root ; COMMAND=/bin/tar -chvzPf /adm/backup/files/dfs_diff_backup_27-10-09.tar.gz -
-newer=.lastfullbackup -T /adm/backup/filelist
Oct 27 14:58:02 dfs sudo:    admin : TTY=unknown ; PWD=/home/admin ; USER=root ; COMMAND=/etc/init.d/mysql start

Using it as root without sudo:

Code:
Oct 28 12:17:01 dfs /usr/sbin/cron[2853]: (root) CMD (/adm/backup/backup.sh)

# 4  
Old 10-28-2009
Did you check the mails of user root? cron (usually) sends any error messages (even those not in syslog) via mail to the user who's running a command (in this case: root).
# 5  
Old 10-28-2009
Yes, but the error was too simple to see it. Smilie
I hadn't used the absolute path for the time file (.lastfullbackup), so it was not found.
Thx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need assistance to define path on a script which cron will execute

I have written a script to execute some sql statement via executable file. It is working fine via command line, however when I schedule it in cron. The executable file is looking for library file in its root directory. Wonder where does cron run the script from, and can we get the script... (4 Replies)
Discussion started by: jaapar
4 Replies

2. Shell Programming and Scripting

Batch script to execute shell script in UNIX server

Hi team, My requirement is to transfer pdf files from windows machine to unix server and then from that unix server we should sftp to another server. I have completed the first part i.e From windows to using to unix server with the help of psftp.exe code: psftp user@host -pw password <... (1 Reply)
Discussion started by: bhupeshchavan
1 Replies

3. Solaris

cannot execute sqlplus in shell script under cron

Hi, I have a bourne shell script that needs to login into sqlplus and execute a stored procedure. I need this shell script to be run under a user crontab. It works fine if run under normal shell prompt. But it gave error message in my log file when run under crontab: SQL*Plus: Release... (4 Replies)
Discussion started by: joe_x
4 Replies

4. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

5. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

6. Solaris

anomalous behaviour when I execute the script via cron

Hi , I am having a strange problem with my cron job : here is the script that I am scheduling from cron WrapperScript.ksh #!/bin/ksh ########## Global Variables ########## DATE=$(date +"%b-%d-%y") TIME=$(date +"%H:%M") LOGFILE=sonar_execution_$DATE.log ########## Main Processing... (2 Replies)
Discussion started by: alookachaloo
2 Replies

7. Shell Programming and Scripting

cron does not execute script properly

I have a simple script that checks for certain printers and records them to a file. When I run the script manually at the command prompt, it works perfect, but when I run the script via cron, nothing happens. No errors reported, and no records are written out. I'm using Solaris 10. Below is the... (4 Replies)
Discussion started by: lmatlebyane
4 Replies

8. UNIX for Dummies Questions & Answers

a cron job needs a perl script to execute

Hello evreyone, this is my first post, and to say i'm new to this is an understatement. I know very little about perl scripts and hope some one can help me. i'm looking to get a script that a cron job can execute. what the script needs to to is 1) connect to a mysql database 2) go to a... (2 Replies)
Discussion started by: Exader
2 Replies

9. Shell Programming and Scripting

script execute by cron problem, but manual ok

Hi; I'm facing the problem with my script like below 30 0 * * * /data/SCRIPT/LOADLOGS/loadata1.sh 2 1 >> /tmp/loaddata.txt loadata1.sh calling a another 2 scripts which we need to exe in sequence. when it perform by cron jon, sometimes it just can process 20 recs or less or nothing but... (6 Replies)
Discussion started by: izai
6 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question