-1

I tried three ways of emptying many files at once with the tee command, but nothing is working. What can be going wrong?

[root@APPSERVER11-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9223905280 Sep 11 12:47 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9468202 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# echo -n | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224224768 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9493203 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# printf '' | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224380416 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9502868 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

It's not working individually as well:

[root@APPSERVER11-S1 ~]# echo > /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9236357120 Sep 11 12:58 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root     520807 Sep 11 12:58 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root   10264642 Sep 11 12:54 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

The truncate command, as instructed in the comments, didn't work as well:

[root@APPSERVER11-S1 ~]# truncate -s 0 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/
total 8292
-rw-r--r-- 1 root root 9283125248 Sep 11 13:32 rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    6026198 Sep 11 13:32 Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 13:06 Services-APIGOLDNODE-1.log.1

The tee version is:

[root@APPSERVER11-S1 ~]# tee --version
tee (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker, Richard M. Stallman, and David MacKenzie.

The filesystem is xfs.

[root@APPSERVER11-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  7.3G     0  7.3G   0% /dev
tmpfs                          tmpfs     7.3G     0  7.3G   0% /dev/shm
tmpfs                          tmpfs     7.3G  185M  7.1G   3% /run
tmpfs                          tmpfs     7.3G     0  7.3G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G   14G  1.8G  89% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/0
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/1002

I tried in another server and only the most constantly updated file (a lot of lines each second) didn't get zeroed. Could it be that the file is so constantly updated it can't even get zeroed?

[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 2046352
-rw-r--r-- 1 root root     3920496 Sep 11 14:35 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30891423129 Sep 11 15:27 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root   406151168 Sep 11 15:27 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log
[root@APPSERVER21-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 492
-rw-r--r-- 1 root root           0 Sep 11 15:28 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30892328752 Sep 11 15:28 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root           0 Sep 11 15:28 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log

This second server's /partition filesystem is xfs, like the first one.

[root@APPSERVER21-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  3.6G     0  3.6G   0% /dev
tmpfs                          tmpfs     3.6G     0  3.6G   0% /dev/shm
tmpfs                          tmpfs     3.6G  137M  3.4G   4% /run
tmpfs                          tmpfs     3.6G     0  3.6G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G  3.0G   13G  20% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     722M     0  722M   0% /run/user/0
JM Calil
  • 9
  • 5
  • 2
    Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own (**no images**) to your question to show at least the research effort you made to solve this yourself. – Cyrus Sep 11 '18 at 15:55
  • 1
    See [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) on [meta]. Note that the [mcve] guidelines call for enough details to reproduce a problem to be included **in the question itself**. Content behind a link is not part of the question. – Charles Duffy Sep 11 '18 at 16:05
  • BTW, `tee` is not part of bash -- if we're going to address a bug in `tee`, we need to know which OS you're running so we can look up which version of `tee` that operating system shipped. (On a GNU system, `tee --version` may have pertinent output; if there *is* no `--version`, that at least tells us it's a non-GNU version of `tee`). – Charles Duffy Sep 11 '18 at 16:12
  • Added tee version – JM Calil Sep 11 '18 at 16:18
  • Ahh. Okay, that's actually helpful. Not likely to get you an answer you want, but helpful. Looks like the filesystem is ignoring `open(..., O_TRUNC)`. What kind of mount point are these files on? Do you get the same problem dealing with files in, say, `/dev/shm`? – Charles Duffy Sep 11 '18 at 16:24
  • Do you not have the `truncate` command? It's in GNU coreutils, and is the tool for this purpose. – Toby Speight Sep 11 '18 at 16:26
  • If this is, say, a FUSE filesystem that doesn't support `open(..., O_TRUNC)`, I'd be surprised if it implemented `truncate()` either. – Charles Duffy Sep 11 '18 at 16:28
  • I tried `truncate` with one of the log files but it didn't work. Added it to the question. – JM Calil Sep 11 '18 at 16:33
  • @JMCalil, ...again, what filesystem is this on? And what are the flags (as shown by `lsattr`)? Some filesystems support marking a file append-only, and that's a reasonable flag to set for something intended to be a log (though `chattr +a` will typically result in `EPERM` when using those flags/calls, rather than their being silently ignored, that's something different filesystems may implement differently). – Charles Duffy Sep 11 '18 at 17:27
  • BTW, if this *is* filesystem-level, you could try truncating the file with Python or some other language -- if that fails too, you know it's not a bash problem or a tee problem (and should probably talk to your sysadmin). – Charles Duffy Sep 11 '18 at 17:33
  • Added the filesystem – JM Calil Sep 11 '18 at 17:56
  • ...and `lsattr` output? (Per https://serverfault.com/questions/461786/restrict-file-access-to-append-only, putting a file into append-only mode on XFS *is* supported). – Charles Duffy Sep 11 '18 at 17:56
  • ...ahh, looks like it may be `xfs_io lsattr` you need to use rather than the `lsattr` used for ext[34]. – Charles Duffy Sep 11 '18 at 18:00
  • `lsattr` returned no attributes for all the files inside /etc/vinter/logs. How can I use `xfs_io lsattr`? – JM Calil Sep 11 '18 at 19:12
  • `xfs_io` is a command that comes with the XFS userspace tools. It has a subcommand named `lsattr`. See `man xfs_io`. – Charles Duffy Sep 11 '18 at 19:47
  • (That said, if this is a fruitful line of investigation, the question is quite clearly off-topic here on SO, and a better fit for either [unix.se] or [ServerFault](https://serverfault.com/); SO's scope is limited to questions about *writing code*, whereas what you have is a system that's configured to disallow *any* code, in any language, from performing the operation you want). – Charles Duffy Sep 11 '18 at 19:48

1 Answers1

0

tee is used to be able to see what is going into a file at the same time the file is being updated. Not really useful for zero-ing out a file.

If you want to zero out a file, use the "> file" command. If you want to zero out multiple files, I recommend a loop. Putting a wildcard into file may cause an error message about ambiguous redirects. Something like the following should work:

for f in /path/to/foo*; do
  >${f}
done

Hope this helps

Lewis M
  • 550
  • 3
  • 7
  • 1
    `>"$f"` would be more correct. The curly braces don't do anything important in the use case at hand (where there aren't any adjoining characters which need to be disambiguated from part of the variable name), but in some versions of bash, you can get a "ambiguous redirect" error without the quotes if the filename contains spaces. – Charles Duffy Sep 11 '18 at 16:04
  • The image contents are too long to be included in a comment. I put your example into an .sh file and changed /path/to/foo* to /etc/vinter/logs/*. Tried with and without braces and none worked. – JM Calil Sep 11 '18 at 16:13
  • Nothing in your images is too long to include. Copy (as text), paste (as text), select the text and click the `{}` button in the editor to format as code. – Charles Duffy Sep 11 '18 at 16:14