Crontab command substitution problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Crontab command substitution problem
# 1  
Old 12-22-2009
Crontab command substitution problem

Hi all,

I am using Ubuntu 8.04 Linux. I have a cron job entry to run a shell script at a scheduled time. This script has a X application to be launched. As cron does not pick already exported environment variables and it runs in a separate environment, I need to export the DISPLAY variable to run my GUI process. So I am setting the DISPLAY environment variable through command substitution at the top of my script as follows:
Code:
export DISPLAY="`who am i | cut -d"(" -f2 | cut -d")" -f1`"

It seems, this does not work with cron (i.e) cron is not executing command substitution inside shell script or at the crontab's "command field" entry (i.e):

crontab -l output:
Code:
SHELL=/bin/bash

01 17 * * * export DISPLAY="`who am i | cut -d"(" -f2 | cut -d")" -f1`" && gui_process.sh &> error.log

I have no other option other than hardcoding the (literal) value instead of command substitution inside my script "gui_process.sh" as follows:
Code:
#!/bin/bash

export DISPLAY="xx.xx.xx.xx:0.0"
xterm +hold -e "GUI"

My questions are:

a) Why cron does not support "command substitution"?

b) Also, I have exported DISPLAY in my .bashrc file (since my login shell is bash) and I have specified this shell at the top of the crontab as: SHELL=/bin/bash. Still why cron is not reading it from ~/.bashrc while executing my script??

c) Since cron is not associated to any terminal (/dev/tty) I guess no need for trapping HUP signal?

Thank you

Last edited by royalibrahim; 12-22-2009 at 07:18 AM..
# 2  
Old 12-22-2009
First off - ALL comands in a cron entry or in a cron script should have explicit paths to command files. The PATH is not the same as you have at the command line.

who am i can be implemented all kinds of ways, as an alias or some other feature of the who command. It is usually this:
Code:
/usr/bin/who am i

where "am and "i" are arguments.

You need to experiment with each simple element of your command in crontab entry - enter the command let it run, remove it from crontab, then check the log or email.

example
Code:
* * * * * /usr/bin/who am i >> /home/royalibrahim/logfile

wait a minute, remove the crontab entry and then read the logfile. Proceed iteratively.



Is there some reason not to use the shell variable $USER?
# 3  
Old 12-22-2009
Th command "who am i" will fail when run from cron. It needs a terminal.
X Windows also needs a terminal.
What are you trying to achieve?
# 4  
Old 12-23-2009
Quote:
Originally Posted by methyl
Th command "who am i" will fail when run from cron. It needs a terminal.
X Windows also needs a terminal.
What are you trying to achieve?
You can see the commands "xterm +hold -e", that I am launching xterm terminal to paint the X application.

Last edited by royalibrahim; 12-23-2009 at 08:04 AM..
# 5  
Old 12-23-2009
  • cron never supported command substitution. It can only "support" it as far as the used shell (/bin/sh) supports it.
  • cron doesn't care for you .bashrc, or any other login file for that matter, because it won't start a login shell. What it will do is basically
    Code:
    /bin/sh -c "your command"

    at the specified time. As an example, this is the environment of a command started by cron on Linux:
    Code:
    SHELL=/bin/sh
    USER=pludi
    PATH=/usr/bin:/bin
    PWD=/home/pludi
    SHLVL=1
    HOME=/home/pludi
    LOGNAME=pludi
    _=/usr/bin/env

    See the minimal path? That's all you have at the start of your script.
  • If you want to start an XTerm on a remote display for a certain user, you'll need to know where to connect to. But that's an information that cron couldn't possibly find out, as it will run even if you aren't connected. It will even run on Sundays, when (probably) no one is in the office. Where should the terminal be shown at then?
# 6  
Old 12-23-2009
Quote:
Originally Posted by pludi
  • cron never supported command substitution. It can only "support" it as far as the used shell (/bin/sh) supports it.
  • If you want to start an XTerm on a remote display for a certain user, you'll need to know where to connect to. But that's an information that cron couldn't possibly find out, as it will run even if you aren't connected. It will even run on Sundays, when (probably) no one is in the office. Where should the terminal be shown at then?
What if I am running only on my local machine (i.e) launching the xterm terminal in my local system - not on the remote systems?

So, command substitution is not possible in whatever be the case, right? what if scripts that are need to be run by cron, have the need of command substitution? How to achieve this? Is this a deficiency in cron - is that incapable to do that?
# 7  
Old 12-23-2009
What is the full list of environment variables that can be set for crontab other than HOME,LOGNAME,PATH,SHELL,MAILTO variables?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Variable substitution problem

HI i was studying about variable substitution. below are info which was given in a online tutorial. ${parameter:-word}---> If parameter is null or unset, word is substituted for parameter. The value of parameter does not change. ${parameter:=word}---> If parameter is null or... (3 Replies)
Discussion started by: scriptor
3 Replies

2. Shell Programming and Scripting

Problem with running the "autorep" command via crontab

Hi, The user "MadeInGermany" tried to help on the below post by saying "This has been asked before; see the links below. Get your current LD_LIBRARY_PATH and redefine that in your ksh script! " Thanks for the help. but this did not help. And my post got locked. I can't reply on my previous... (5 Replies)
Discussion started by: girish1428
5 Replies

3. Shell Programming and Scripting

Another substitution problem

Hello again, I'm trying to change the following line: INSERT INTO PH1_TX_LOAD VALUES ('TX-78731-AABSS:4182-4','RH: GUIDE TO TENNIS',TO_DATE('18-JUN-2001:00:00:00', 'DD-MON-YYYY:HH24:MI:SS'),TO_DATE('21-JUN-2001:00:00:00', 'DD-MON-YYYY:HH24:MI:SS'),500) so that any TO_DATE is taken... (6 Replies)
Discussion started by: user_invalid
6 Replies

4. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

5. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

6. Shell Programming and Scripting

sed substitution problem

Can anyone please help me on this. i have a file with lines say X X3200 X X X X2400 X X4100 I want to use sed to put the numbers in braces. the output should be like, X X(3200) X X X X(2400) X X(4100) (7 Replies)
Discussion started by: diliphp
7 Replies

7. Shell Programming and Scripting

problem in embeded command substitution

$ echo $(tty|sed 's#/*/##') pts/0 $ who | grep $(tty|sed 's#/*/##') grep: 0652-033 Cannot open 0551-011. grep: 0652-033 Cannot open Standard. grep: 0652-033 Cannot open input. grep: 0652-033 Cannot open is. grep: 0652-033 Cannot open not. grep: 0652-033 Cannot open a. grep: 0652-033 Cannot... (2 Replies)
Discussion started by: wrl
2 Replies

8. AIX

VI Substitution problem

I'm having a problem getting my variables to work in dishing out an RMC script. The $1 works fine. $2 does not Here's a portion of the script: server=$1 filesystem1=$2 # dsh -w $1 'mkcondition -c "/var space used" -s "Name == \"$2\"" -e "PercentTotUsed > 90" -d "An event will be generated... (7 Replies)
Discussion started by: gravy26
7 Replies

9. Shell Programming and Scripting

Substitution problem.

Hi, Just need some help in this. Suppose there is one file a.txt, which contains this data: "25187","00000022","00",28-MAR-2007,"" ,"D",-000001550,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,29-MAR-2007 613TB.STEXTRF1 "25187","0000004H","00",29-MAR-2007,""... (3 Replies)
Discussion started by: er_ashu
3 Replies

10. UNIX for Dummies Questions & Answers

Having Problem with crontab command

Hi all , When i am tryting to execute crontab from home directory for a shell script which is located in some directory it is giving a error message The crontab file is 0 9-17 * * 1-5 /mydir/myshell The following output is given Your "cron" job /mydir/myshell produced the following... (1 Reply)
Discussion started by: arunava_maity
1 Replies
Login or Register to Ask a Question