Permission denied

Trying to get date into the txt file.

It says

Permission denied.

echo $(date +%I:%M:%S_%D) >> /tmp/systemd_suspend_test_err.txt
exec 2>> /tmp/systemd_suspend_test_err.txt

if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here
  echo "we are suspending $(date +%I:%M:%S_%D)." 
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here
   echo "and we are back from being suspended $(date +%I:%M:%S_%D)" 
  # gxmessage works IF xhost + is run first.
  gxmessage -timeout 5 -fg red -display :0 'Back from being suspended.'
  #ffplay /usr/share/sounds/My_Sounds/Relax_6_Seconds.mp3
  mplayer /usr/share/sounds/My_Sounds/Relax_6_Seconds.mp3
fi

Does the user you are logged into pass this test?
set filename to whatever file you want then:

[ -w $filename ] && echo "Writable" || echo "Not Writable"

>> opens a file append (open file, move file pointer to the very end of the file), if the file does not exist it tries to create the file and opens it for write at position zero. Any of these steps can give the error you got.

What do you get? Note: Are you creating new file?? ... you need write privilege to the directory, otherwise just write privilege to the file itself and a minimum of execute on the path.

2 Likes

I got Writeable.

I went ahead and used this which works.

echo $(date +%I:%M:%S_%D) >> /home/andy/Downloads/systemd_suspend_test_err.txt
exec 2>> /home/andy/Downloads/systemd_suspend_test_err.txt

--- Post updated at 08:56 PM ---

Why is this putting in the date twice?

echo $(date +%I:%M:%S_%D) >> /tmp/systemd_suspend_test_err.txt 
exec 2>> /tmp/systemd_suspend_test_err.txt

I do not know a good answer, I am guessing:

>> uses an existing file if one is available, it does not clear out old data in the file like > does.

If the dates are precisely the same down to the second then I do not see why. If you rerun your code then the first, older, timestamp should be at least a few seconds in the past.

$ date +%I:%M:%S_%D
09:18:14_04/05/19
1 Like

Open the file with > and previous contents is overwritten.
BTW if you permanently open it with exec then you can do this

exec 2> /tmp/systemd_suspend_test_err.txt
date "+%I:%M:%S_%D" >&2

Note that the exec opens the file, either open-truncate with > or open-append with >>, and associates descriptor 2=stderr with it.
Subsequent writes to stderr go to the stream i.e. append.

1 Like

I forgot that it is sending date both before and after computer suspension.