-2

Hello this is purely practicing my cronjob Here is my bash script below :

#!/bin/bash
grep $1 $2
rc=$?

if [ $rc != 0 ]
then
        echo "specified string $1 not present in $2"
else
        echo "specified string $1 is present in the file $2"
fi

# number of lines of in a file
wc -l  $2 | awk '{print $1}'

Here is my crontab below :

20 16 * * * /home/mbarrett/Desktop/ ./find_string.sh sam text_string.file > /var/log/backupstring.log 2>&1

Any advice to what I may be doing wrong?

Barmar
  • 741,623
  • 53
  • 500
  • 612
malik
  • 31
  • 6
  • 1
    `Any advice to what I may be doing wrong?` Depends on what you want to do. I don't see a real question here. Please describe your problem. – Socowi Oct 25 '18 at 19:18
  • I want to get the output of my script and put it into a log but my when i open the log there is no output – malik Oct 25 '18 at 19:20
  • Looks fine to me. Does your user under which that crontab is running have access to write to `/var/log/backupstring.log`? – JNevill Oct 25 '18 at 19:21
  • Yes the user I am under has all the permissions but there is nothing in the log – malik Oct 25 '18 at 19:23
  • 1
    I don't no much about `cron`, but that space seems strange. Are you sure you wanted to write `...Desktop/ ./find_string.sh` and not `...Desktop/find_string.sh`? – Socowi Oct 25 '18 at 19:26
  • I thought that I need to gave it a path to the script/command that i want to run or is there another way to get the output to log? – malik Oct 25 '18 at 19:35
  • Do you think I should use the pipe command ( | ) in this situation ? – malik Oct 25 '18 at 19:43
  • Possible duplicate of [How to log cron jobs?](https://stackoverflow.com/q/4811738/608639), [Managing log files created by cron jobs](https://stackoverflow.com/a/41756145/608639), [How to redirect output to a file from within cron?](https://unix.stackexchange.com/q/52330/56041), etc. – jww Oct 25 '18 at 20:19
  • 1
    @malik Yes, you need to give the path to the program you want to run. But the space ends that path, so it's trying to execute the `Desktop` folder as a program. – Barmar Oct 25 '18 at 20:26
  • @jww That may be what he's asking, but the example crontab entry shows that his problem has nothing to do with the redirection, it's with basic command syntax. – Barmar Oct 25 '18 at 20:26

1 Answers1

-1

You have the Desktop directory at the beginning of your command, which will try to execute it as a program, which won't work. If you want to run the script from that directory, you need to execute the cd command to change to it.

20 16 * * * cd /home/mbarrett/Desktop/; ./find_string.sh sam text_string.file > /var/log/backupstring.log 2>&1

Also, get in the habit of always quoting your variables in the script. Your script as written won't work if the pattern or filename contains whitespace.

Barmar
  • 741,623
  • 53
  • 500
  • 612