0

I have written a script and I want to schedule it to run at a certain time with the at command. I also want to use an argument in order to pass the time I want in the script so it can be executed at a specific time.

The script's name is displaydir and it has the following arguments and form:

./displaydir dir1 09:00 AM

The script:

#!/bin/bash
at $2 $3
ll $1
cp ./$1/ ./dir2
exit 0
Socowi
  • 25,550
  • 3
  • 32
  • 54
user08
  • 21
  • 1
  • 1
  • 6

2 Answers2

1

To run in every week you should rewrite your script like this:

#!/bin/bash
ll $1
cp ./$1/ ./dir2
exit 0

The place of at command is not there.

And you need to add record in cron with command crontab -e

0 18 * * 1 /path/to/displaydir argument

Not sure if you can add argument when run command with at

And the output of this script shoudl be redirected somewhere as at and cron run in background and do not print info on terminal/screen

The format of at command is:

at -f /path/to/displaydir 6:00PM

This will run script /path/to/displaydir at 18:00h

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
  • Thank you, is there any way that the at command can be in the script? – user08 Dec 27 '19 at 17:01
  • @user08, it can be. But what is the reason? ANd the time is define in cron record `0 18 * * 1`, run 6:00PM every monday – Romeo Ninov Dec 27 '19 at 17:03
  • Could you show me how it could be in the script? because when I run it with the at command in the script,the script is executed in real time and not the certain time that the user has chosen – user08 Dec 27 '19 at 17:05
0

There are several problems here. The one that's causing the immediate problem is that at reads the commands to run from its standard input, and that's not (generally) the script. Generally the simplest way to feed input to a command in a script (as its standard input) is a here-document, like this:

command <<EOF-MARKER
input
more input
EOF-MARKER
nextCommand

The next problem is that the cp command is being asked to copy a directory, but hasn't been passed the -R option, so it'll refuse to do anything. I think you probably want cp ./$1/* ./dir2 to copy the files in the directory, rather than the directory itself.

I'm not sure about this, but if ll is an alias, it may not work in the at job (since aliases are generally disabled in non-interactive shells). I'd recommend using the full command (probably ls -l).

Next, the output from the job will be emailed to you. Or at least, it'll try to email it to you; it might or might not do anything useful. If you want the output to go somewhere predictable, you should redirect it yourself.

Finally, if the directory name $1 has any funny characters in it, using it without quotes will cause trouble. (I work mostly on macOS, where filenames with spaces are very common, so I'm ... aware of this issue.) Normally, the best way to handle this is to double-quote the parameter reference, but in a here-document single-quotes are more likely to work (unless the name might itself contain single-quotes).

So, here's what I get for the corrected script:

#!/bin/bash
at $2 $3 <<EOF
ls -l '$1'
cp ./'$1'/* ./dir2
exit 0
EOF
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Thank you for answering but for some reason it is not working :( seems I have to do a separate file. – user08 Dec 27 '19 at 19:05