LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Partial list with ls-l in bash script run in cron but full list run from command line (http://www.linuxquestions.org/questions/linux-general-1/partial-list-with-ls-l-in-bash-script-run-in-cron-but-full-list-run-from-command-line-851649/)

redgshost 12-21-2010 02:43 PM

Partial list with ls-l in bash script run in cron but full list run from command line
 
I am not exactly new to linux but I don't have much knowledge of it and this seemed the best place to put this question.

First apologies for the length of this post. I'd like to clearly explain the problem.

I have an unslung nslu2 (slug) on which I have a script that copies large video files to a nas and then lists the files in the source and target directories so that I can check the files are being copied across properly. The script is a simple bash script. The source directory is on a PVR attached to the slug (topfield TF5800) and does not use linux to list the files. This part works perfectly. The target directory is on a nas running linux (terastation home server with root access). I mount the nas directory as a directory on the slug and this bit works fine. I list the files in the nas directory using linux (ls and dir) and it is this part that is giving problems.

When I run the script on the slug from the command line, logged on as root, it works perfectly and is able to email me a complete, formatted list of files in the target directory using either ls-l or dir-l. ls-l is better because the dir-l leads to lots of escape characters. However if the script is run from cron, as root, then the dir-l command leads to an unformatted list with lines running one into another with no end of line carriage return. The ls-l command leads to a seemingly random list of a subset of the files in the directory. Whilst the subset does appear random it is consistent from one run to the next. I have confirmed it is a subset by looking in the directory from the command line and also ftp'ing in to the nas. The video files play properly they just aren't listed properly when the script is run from cron.

I have tried several ploys to get around this including using cd within the script to change to the correct directory, within the script unmounting and mounting the target directory and even creating a pause but none of this seems to work and I still get the result described above.

I have read elsewhere that it is neccesary to put the full path for commands or set environment variables. The script does work partially so it seems to me that neither of these can be the solution. As I said at the start, I'm not exactly new to linux but I don't know much and I have no further ideas remaining, can anyone offer any help? Sorry for the long post.

Red

kbp 12-21-2010 04:17 PM

Could you post the output of 'aliases' ?

redgshost 12-22-2010 02:59 PM

Sorry for my ignorance but what do you mean by 'aliases'?

Red

kbp 12-22-2010 06:24 PM

Type the word 'aliases' in a terminal and press enter.

redgshost 12-22-2010 06:27 PM

Thanks. In response to aliases I get the following response

aliases: No such file or directory

Red

kbp 12-22-2010 06:30 PM

[doh!] .. sorry, that was my fault, the command is 'alias'

redgshost 12-23-2010 01:25 PM

Thanks. I typed 'alias' and it did nothing, just skipped to a new command prompt. I am logged in as root.

Red

szboardstretcher 12-23-2010 01:29 PM

In your cron script... specify the absolute path of ls... such as:

/bin/ls -l

Then, on your command line, do the same thing.

/bin/ls -l

This is a workaround, that will skip any aliases or differing versions of ls.

redgshost 12-23-2010 07:12 PM

Thanks for the tip to try the full path. I modified the script and let it run as a cron job. Unfortunately it did not fix the problem. I got the same partial list as before.

Is there anything else I should try?

Red

szboardstretcher 12-27-2010 12:40 PM

Quote:

Originally Posted by redgshost (Post 4201591)
Thanks for the tip to try the full path. I modified the script and let it run as a cron job. Unfortunately it did not fix the problem. I got the same partial list as before.

Is there anything else I should try?

Red

Could you provide me a few things as root? Obviously you can scrub any output that you do not wish to share.

1) Output of the command on command line
2) Outpuf of the command in the script
3) Output of 'set'
4) Output of 'whereis ls' and 'which ls'

Hopefully with these, we can figure out what in the world is going on.

redgshost 12-30-2010 01:50 AM

Apologies for the delay responding. My attention has been taken elsewhere over the holiday.

The output you asked for is as follows. Unfortunately two of the commands were not recognised by my slug. Thank you very much for your help with all this.

Red

Quote:

Welcome to Unslung V2.3R63-uNSLUng-6.8-beta

---------- NOTE: THIS SYSTEM IS CURRENTLY UNSLUNG ----------


BusyBox v0.60.4 (2005.03.22-06:52+0000) Built-in shell (ash)
Enter 'help' for a list of built-in commands.

# help

Built-in commands:
-------------------
. : alias break builtin cd chdir continue eval exec exit export
false fc hash help jobs let local pwd read readonly return set
setvar shift times trap true type ulimit umask unalias unset
wait

# set
SSH_CLIENT=xx
USER='root'
MAIL='/var/mail/root'
HOME='/root'
SSH_TTY='/dev/ttyp0'
PS1='# '
OPTIND='1'
PS2='> '
LOGNAME='root'
TERM='xterm'
PPID='953'
PATH='/opt/sbin:/opt/bin:/usr/sbin:/usr/bin:/sbin:/bin'
SHELL='/bin/sh'
IFS='
'
PWD= xx
SSH_CONNECTION= xx


# whereis ls
whereis: No such file or directory


# which ls
which: No such file or directory


Output of script run from command line

Using dir-l

total 60120998
-rwx------ 1 root root 2676783104 Dec 10 11:03 Alvin\ and\ the\ Chipmunks_101128.rec
-rwx------ 1 root root 2489950208 Dec 14 10:06 Any\ Human\ Heart_101121.rec
-rwx------ 1 root root 2074701824 Dec 14 11:01 Any\ Human\ Heart_101128.rec
-rwx------ 1 root root 2171054080 Dec 14 11:55 Any\ Human\ Heart_101205.rec
-rwx------ 1 root root 2069696512 Dec 14 12:45 Any\ Human\ Heart_101212.rec
-rwx------ 1 root root 1868810240 Dec 10 09:49 Babe_101114.rec
-rwx------ 1 root root 3502274560 Dec 1 11:29 Close\ Encounters\ of\ the\ Third\ Kind_101120.rec
-rwx------ 1 root root 2403512320 Dec 1 12:25 Dr\ No_101123.rec
-rwx------ 1 root root 2435854336 Dec 9 10:00 From\ Russia\ with\ Love_101202.rec
-rwx------ 1 root root 3909341184 Dec 9 11:29 Goldfinger_101127.rec
-rwx------ 1 root root 2066905088 Dec 13 10:59 Ladies\ in\ Lavender_101204.rec
-rwx------ 1 root root 2687948800 Dec 13 10:08 Mrs\ Doubtfire_101204.rec
-rwx------ 1 root root 4439422976 Dec 11 12:34 North\ by\ Northwest_101122.rec
-rwx------ 1 root root 2137460736 Dec 19 10:04 Singin'\ in\ the\ Rain_101125.rec
-rwx------ 1 root root 3166148608 Dec 11 14:02 Sweeney\ Todd\ The\ Demon\ Barber\ of\ Fleet_101127.rec
-rwx------ 1 root root 2301673472 Dec 12 13:12 Tea\ with\ Mussolini_101204.rec
-rwx------ 1 root root 3492745216 Dec 11 10:40 The\ Mission_101116.rec
-rwx------ 1 root root 3814914048 Dec 13 12:26 The\ Perfect\ Storm_101208.rec
-rwx------ 1 root root 2629425152 Dec 1 10:06 The\ Spiderwick\ Chronicles_101114.rec
-rwx------ 1 root root 1730394112 Dec 9 12:09 The\ Witches_101128.rec
-rwx------ 1 root root 3728764928 Dec 12 12:17 Thirteen\ Days_101124.rec
-rwx------ 1 root root 3766112256 Dec 12 10:38 Those\ Magnificent\ Men\ in\ Their\ Flying\ M_101203.rec
drwx------ 1 root root 4096 Aug 12 00:23 avi
drwx------ 1 root root 4096 Jun 21 2009 mpg


Using ls-l

total 60120998
-rwx------ 1 root root 2676783104 Dec 10 11:03 Alvin and the Chipmunks_101128.rec
-rwx------ 1 root root 2489950208 Dec 14 10:06 Any Human Heart_101121.rec
-rwx------ 1 root root 2074701824 Dec 14 11:01 Any Human Heart_101128.rec
-rwx------ 1 root root 2171054080 Dec 14 11:55 Any Human Heart_101205.rec
-rwx------ 1 root root 2069696512 Dec 14 12:45 Any Human Heart_101212.rec
-rwx------ 1 root root 1868810240 Dec 10 09:49 Babe_101114.rec
-rwx------ 1 root root 3502274560 Dec 1 11:29 Close Encounters of the Third Kind_101120.rec
-rwx------ 1 root root 2403512320 Dec 1 12:25 Dr No_101123.rec
-rwx------ 1 root root 2435854336 Dec 9 10:00 From Russia with Love_101202.rec
-rwx------ 1 root root 3909341184 Dec 9 11:29 Goldfinger_101127.rec
-rwx------ 1 root root 2066905088 Dec 13 10:59 Ladies in Lavender_101204.rec
-rwx------ 1 root root 2687948800 Dec 13 10:08 Mrs Doubtfire_101204.rec
-rwx------ 1 root root 4439422976 Dec 11 12:34 North by Northwest_101122.rec
-rwx------ 1 root root 2137460736 Dec 19 10:04 Singin' in the Rain_101125.rec
-rwx------ 1 root root 3166148608 Dec 11 14:02 Sweeney Todd The Demon Barber of Fleet_101127.rec
-rwx------ 1 root root 2301673472 Dec 12 13:12 Tea with Mussolini_101204.rec
-rwx------ 1 root root 3492745216 Dec 11 10:40 The Mission_101116.rec
-rwx------ 1 root root 3814914048 Dec 13 12:26 The Perfect Storm_101208.rec
-rwx------ 1 root root 2629425152 Dec 1 10:06 The Spiderwick Chronicles_101114.rec
-rwx------ 1 root root 1730394112 Dec 9 12:09 The Witches_101128.rec
-rwx------ 1 root root 3728764928 Dec 12 12:17 Thirteen Days_101124.rec
-rwx------ 1 root root 3766112256 Dec 12 10:38 Those Magnificent Men in Their Flying M_101203.rec
drwx------ 1 root root 4096 Aug 12 00:23 avi
drwx------ 1 root root 4096 Jun 21 2009 mpg



Output of script run from cron

Using dir-l

total 60120998
-rwxr-xr-x 1 root root 2676783104 Dec 10 11:03 Alvin\ and\ the\ Chipmunks_101128.rec -rwxr-xr-x 1 root root 2489950208 Dec 14 10:06 Any\ Human\ Heart_101121.rec -rwxr-xr-x 1 root root 2074701824 Dec 14 11:01 Any\ Human\ Heart_101128.rec -rwxr-xr-x 1 root root 2171054080 Dec 14 11:55 Any\ Human\ Heart_101205.rec -rwxr-xr-x 1 root root 2069696512 Dec 14 12:45 Any\ Human\ Heart_101212.rec -rwxr-xr-x 1 root root 1868810240 Dec 10 09:49 Babe_101114.rec -rwxr-xr-x 1 root root 3502274560 Dec 1 11:29 Close\ Encounters\ of\ the\ Third\ Kind_101120.rec -rwxr-xr-x 1 root root 2403512320 Dec 1 12:25 Dr\ No_101123.rec -rwxr-xr-x 1 root root 2435854336 Dec 9 10:00 From\ Russia\ with\ Love_101202.rec -rwxr-xr-x 1 root root 3909341184 Dec 9 11:29 Goldfinger_101127.rec -rwxr-xr-x 1 root root 2066905088 Dec 13 10:59 Ladies\ in\ Lavender_101204.rec -rwxr-xr-x 1 root root 2687948800 Dec 13 10:08 Mrs\ Doubtfire_101204.rec -rwxr-xr-x 1 root root 4439422976 Dec 11 12:34 North\ by\ Northwest_101122.rec -rwxr-xr-x 1 root root 2137460736 Dec 19 10:04 Singin'\ in\ the\ Rain_101125.rec -rwxr-xr-x 1 root root 3166148608 Dec 11 14:02 Sweeney\ Todd\ The\ Demon\ Barber\ of\ Fleet_101127.rec -rwxr-xr-x 1 root root 2301673472 Dec 12 13:12 Tea\ with\ Mussolini_101204.rec -rwxr-xr-x 1 root root 3492745216 Dec 11 10:40 The\ Mission_101116.rec -rwxr-xr-x 1 root root 3814914048 Dec 13 12:26 The\ Perfect\ Storm_101208.rec -rwxr-xr-x 1 root root 2629425152 Dec 1 10:06 The\ Spiderwick\ Chronicles_101114.rec -rwxr-xr-x 1 root root 1730394112 Dec 9 12:09 The\ Witches_101128.rec -rwxr-xr-x 1 root root 3728764928 Dec 12 12:17 Thirteen\ Days_101124.rec -rwxr-xr-x 1 root root 3766112256 Dec 12 10:38 Those\ Magnificent\ Men\ in\ Their\ Flying\ M_101203.rec
drwxr-xr-x 1 root root 4096 Aug 12 00:23 avi
drwxr-xr-x 1 root root 4096 Jun 21 2009 mpg


Using ls-l

-rwxr-xr-x 1 root root 2074701824 Dec 14 11:01 Any Human Heart_101128.rec
-rwxr-xr-x 1 root root 2069696512 Dec 14 12:45 Any Human Heart_101212.rec
-rwxr-xr-x 1 root root 1868810240 Dec 10 09:49 Babe_101114.rec
-rwxr-xr-x 1 root root 2066905088 Dec 13 10:59 Ladies in Lavender_101204.rec
-rwxr-xr-x 1 root root 2137460736 Dec 19 10:04 Singin' in the Rain_101125.rec
-rwxr-xr-x 1 root root 1730394112 Dec 9 12:09 The Witches_101128.rec
drwxr-xr-x 1 root root 4096 Aug 12 00:23 avi
drwxr-xr-x 1 root root 4096 Jun 21 2009 mpg
I hope this is what you were after. As before thank you very much with your help trying to sort this out

Red

catkin 12-30-2010 03:13 AM

Would be nice to find out why this is happening.

Meanwhile it might be worth trying ls -ld * or ls -1 (if you only want the file names).

Alternatively the dir -l output is parseable ...

redgshost 12-30-2010 03:24 PM

Thanks for the comments. I will try the ls -ld * to see if that helps. I won't use ls -1 as I do want the file sizes to check the copy across when ok.

Also I agree the dir output can be made readable but here what I don't understand is why it is laid out neatly when I run the script (as root) from the command line yet when run from cron the whole output is dumped into a single line. I did think some character was being suppressed for this to happen but I cannot understand why this part is different in the different situations.

Anyway thank you for your help. I will try ls -ld * and report back here.

Thank you.

Red

catkin 12-30-2010 09:10 PM

Quote:

Originally Posted by redgshost (Post 4208148)
but I cannot understand why this part is different in the different situations.

It may be that dir is detecting what its stdout is going to with something like the isatty() system call.

redgshost 12-31-2010 01:52 PM

Thanks for all the input.

I tried ls -ld * and the output was just as before (thanks though)

Red


All times are GMT -5. The time now is 08:57 PM.