That may delete and recreate the file and leave the old file still open hogging disk, unfixable without rebooting the daemon or server! Don't do it!
---------- Post updated at 08:38 AM ---------- Previous update was at 08:36 AM ----------
The reason none of the commands using redirection worked is that the redirection happens before the su. So you don't get permission to truncate the file.
To get permissions to run the file, the command to truncate the file will need to run in a shell with permissions to do so.
Putting that in quotes instead of quote tags means when I try and quote you, the error message disappears. Use code tags for code.
"no askpass program" means it needs to ask you for a password and can't because it's not in a terminal. Run it in a terminal.
---------- Post updated at 09:12 AM ---------- Previous update was at 09:11 AM ----------
This won't work, again, because the redirection happens before the sudo. The shell doesn't have permissions to overwrite the file, and will fail to redirect into it.
---------- Post updated at 09:14 AM ---------- Previous update was at 09:12 AM ----------
you are right but if newuser hasnot a entry in sudoers then your example also is not solution to me.
in this case sudo is not required and sudo does not get extra rights on files.
maybe `su` is can be usable instead of sudo.
I can do better than that, I can give you a working example:
## Demo of deleting a logfile in use
$ echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ rm filename # delete filename.
$ ls -l filename # The directory entry will be gone -
ls: cannot access filename: No such file or directory
cat <&5 # ...but the file still exists, since we have it open.
the owls are not what they seem
$ exec 5<&- # Only on close will it TRULY be deleted from disk.
## Demo of truncating a file in use
echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ : > filename # truncate filename
$ ls -l filename # The same file exists, with zero size
-rw-r--r-- 1 username users 0 Oct 7 11:40 filename
$ cat <&5 # Read from the file. Nothing there.
$ exec 5<&- # close the file.
cp might overwrite a file, or might delete and recreate it, I wouldn't depend on either -- implementations of things can vary. But a shell redirect > always truncates.
Also, would cat /dev/null > file
have the same potential problem (sudo/redirect issues aside)?[/code] Nope. cat /dev/null is effectively the same statement as : -- a statement that prints no output. It's the > that's important.
I used to use echo > filename to truncate things until I realized echo does indeed print slightly more than nothing there -- it prints one blank line.