# Difference between using shell or cron to execute a script

**URL:** <https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243>\
**Category:** Shell Programming and Scripting\
**Created:** [October 28, 2009, 6:14am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243 "2009-10-28T06:14:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)\
**Post date:** [October 28, 2009, 6:14am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243/1 "2009-10-28T06:14:58Z")

</div>

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?

```nohighlight
#!/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

```

```nohighlight
 cat /adm/backup/filelist 
/var/lib/mysql
/srv/www/htdocs

```

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [October 28, 2009, 6:52am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243/2 "2009-10-28T06:52:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)\
**Post date:** [October 28, 2009, 7:19am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243/3 "2009-10-28T07:19:01Z")

</div>

I can't see any error in the log. Here the output when executing as a regular user:

```nohighlight
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:

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

```

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [October 28, 2009, 8:11am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243/4 "2009-10-28T08:11:21Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)\
**Post date:** [October 28, 2009, 8:39am UTC](https://community.unix.com/t/difference-between-using-shell-or-cron-to-execute-a-script/252243/5 "2009-10-28T08:39:52Z")

</div>

Yes, but the error was too simple to see it. 😉  
I hadn't used the absolute path for the time file (.lastfullbackup), so it was not found.  
Thx
