0

I am trying to send a mail with at command, but the mail is getting delivered instantly, instead it should send the mail as per the time provided in at command. Below is the script:

#!/bin/bash

TODAY=`date +%Y%m%d`
echo 'Please enter the User-Name:'
read name
echo 'No. of days access is required - number only'
read NumberofDays

echo "Root access revoked for $name" | mailx -s "root access revoked for $name on $TODAY" xyz@example.com| at now + $NumberofDays
Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • 1
    I guess your usage of `at` command here is wrong.As per your last line in the script, the message of `echo` will be piped to the `mailx` command first and then to `at`, that's why the mail is being delivered first and `at` command is ignored. – User123 Dec 14 '18 at 04:35
  • Read [man at(1)](https://linux.die.net/man/1/at), Check the usage of `-f` and `-m` with `at` command. – User123 Dec 14 '18 at 04:37

1 Answers1

1

Notice the difference between these two statements:

  • pipe the output of a command to at
  • pipe a command text to execute to at

The script you showed does the first, and you want to do the second:

cat << EOF | at "now + $NumberofDays days"
echo "Root access revoked for $name" | \
mailx -s "root access revoked for $name on $TODAY" xyz@example.com
EOF

I also added "days" after $NumberofDays (thanks @User123!) in the at command's arguments, otherwise it's not correct syntax for at.

janos
  • 109,862
  • 22
  • 193
  • 214
  • I guess `days` should be added after the variable:`$NumberofDays` in `at` command, like: `at "now + $NumberofDays days"` or else it'll give error. – User123 Dec 14 '18 at 06:04
  • @User123 - Actually I am providing the input manually in minutes as well to check the script, that's why I kept it as it is. – Rahul Jangid Dec 14 '18 at 06:46