0

I made a script called purging.sh. I had used cd command in that script and the script changes directory many a times so that I have assigned some variable to the cd command which in turn help to change the directory.

cd /home/parthipan/test/wireshark/$a/$b

To run the script, I have to use source ./purging.sh or . ./purging.sh.

But I have to schedule the script for that I am using crontab,

10 01 * * * . /home/parthipan/test/purging.sh

Start of my script:

#!/bin/bash
a=$( date -d "`date` -60 days" "+%Y%m%d" | cut -b 1-4 )
b=$( date -d "`date` -60 days" "+%Y%m%d" | cut -b 5-6 )
c=$( date -d "`date` -60 days" "+%Y%m%d" | cut -b 7-8 )
cd /home/parthipan/test/wireshark/$a/$b
d=$( ll | awk -F " " '{print $9}' | awk -v value=$c '$1 < value' | sed "/^$/d" | xargs )
echo "The Days archiving are: $d"
e=$( ll | awk -F " " '{print $9}' | awk -v value=$c '$1 < value' | sed "/^$/d" | wc -l )
f=$( date -d "`date` - 61 days" "+%Y%m%d" | cut -b 5-8 )
if (( $e > 0 ))
        then
                tar --no-overwrite-dir -czvf backup_$f.tar.gz $d
                mv backup_$f.tar.gz /home/parthipan/test/logs_backup
                #rm -rf $d
                logger Data has been purged

The above script will be continued, with end of fi command. It normally works on cli but not working on crontab. I have used logger command in last line so that the message will print in /var/log.
But the message printing is,

May  1 00:50:01 parthipan-nadar systemd: Started Session 24 of user root.

After that no other log forms. but the required log should be Data has been purged.

7
  • 1
    Welcome to the site. If you say "but it is not working", can you specify how it is not working? Is it simply not run, or is it run (and how do you know that) but there is an error?
    – AdminBee
    May 20, 2020 at 13:57
  • 4
    Although you tagged your question bash, remember that cron likely uses /bin/sh - so if your script uses any non-POSIX features it may behave differently when sourced from cron compared to when you source it from your interactive shell May 20, 2020 at 14:12
  • 4
    Why do you need to source the script instead of running it like bash purging.sh? May 20, 2020 at 14:36
  • Does your script have a shebang (#! /bin/bash) as the first line? May 20, 2020 at 15:02
  • steeldriver , you are right but my question is what can i do now ? is there any way to run the script in crontab ? May 20, 2020 at 18:35

1 Answer 1

2

It's working when you source it in your shell because you appear to be using your ll alias in the script. Scripts executing from cron do not have your environment, so you need to be more explicit about setting the PATH (if you're calling executables that live outside of /bin or /usr/bin) and not relying on your specific aliases or environment variables. If your ll alias is an alias for ls -l, then use ls -l instead (but see the first point below).

Some other comments:

  • Don't parse ls. If you need to filter files based on age, use stat or find.

  • You don't need to nest a date call within a date call: use -d "60 days ago"

  • Don't call date 6 times (!!) just to get the year month and day:

    read -r year month day < <(date -d "60 days ago" "+%Y %m %d")
    
  • Use descriptive variable names. a b c ... are bad.

4
  • Thanx for correcting me but one problem remain the same even after doing the changes, and that is cd command. If we have to use cd command in the script then we have to source the script before running it. This is actually creating problem!!! and I have used cd command due to frequently changing in the folders. I have 2020 folder in that I have 01,02...12 folder and in 01 I have 01,02.....31 . Actually this is the basic format. In which i have to take the backup and remove the file. May 20, 2020 at 20:40
  • And you don't show any code in your question about how you use cd. May 20, 2020 at 21:06
  • sorry brother, Now i have modified my question May 21, 2020 at 5:06
  • Your statement is COMPLETELY UNTRUE: "If we have to use cd command in the script then we have to source the script before running it". May 21, 2020 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .