Mailx with at command

123 Views Asked by At

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" [email protected]| at now + $NumberofDays
1

There are 1 best solutions below

2
janos On

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" [email protected]
EOF

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