Crontab not processing parameters sent to script.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Crontab not processing parameters sent to script.
# 1  
Old 05-08-2015
Crontab not processing parameters sent to script.

Hello All,

I'm running Debian on a ThinkPad X1/2G and all seems to be running well.

However, it's got a SSD that needs the trim command run at regular intervals.
I'm implementing this using cron and a shell script, as recommended.

The cron job entries are as follow:
Code:
0 * * * * /etc/cron.hourly/trim /
* 0 * * * /etc/cron.hourly/trim /home

And the script is:
Code:
LOG="/TRIM.LOG"
echo "$(date) Started... $1" >> $LOG

if [ -z "$1" ];
then echo "No parameters" >> $LOG
else echo "Trimming $(fstrim -v $1)" >> $LOG
    fi

I seem to be having a problem getting the parameters from cron
to be recognised by fstrim as parameters in the shell script even though bash
seems to recognise the parameters.

If I run the script './trim /' as root at the command line it works as expected with
bash and fstrim recognising the variables.

Other things tried...
Hard-coding the parameters seems to work.
Checked permissions on script 0755.

Any ideas?
# 2  
Old 05-09-2015
Did you create the normal user's environment for the cron job? cron per default has a very limited environment.
# 3  
Old 05-09-2015
I created a root user file with the 'crontab -e' command line.
The schedule seems to be executing at the right time and
passing the expected arguments to bash successfully.

Is it possible cron is adding invisible formatting to the argument
that bash interprets as normal but fstrim interprets as an error?
# 4  
Old 05-09-2015
Quote:
Originally Posted by ASGR
... ... ...

Is it possible cron is adding invisible formatting to the argument
that bash interprets as normal but fstrim interprets as an error?
Look at your log file. The 2nd line your script:
Code:
"$(date) Started... $1" >> $LOG

includes the argument passed to your script by cron when it writes to the log file.

If you add the line:
Code:
echo "SHELL is $SHELL" >> $LOG

right after the above line in your script, what does it add to your log file when you run your script manually and what does it add to your log file when you run it from cron?

What output are you seeing in your log file from the line:
Code:
else echo "Trimming $(fstrim -v $1)" >> $LOG

What operating system are you using?
# 5  
Old 05-09-2015
You say *If I run the script './trim /' as root at the command line it works as expected with bash and fstrim recognising the variables."

Are these cron jobs in the root crontab? Or running as some other user?

I assume it still runs interactively if you:
Code:
/etc/cron.hourly/trim /

using the full path?
# 6  
Old 06-09-2015
Sorry for the late reply.

hicksd8
-----------
I created the jobs when logged in as root with the 'crontab -e' command. I do get the message that the jobs have been updated and the schedule and script do execute as you would expect.

Running the command with an absolute path, I get the same outcome as I do with a relative path and I get the full output from the command as if it was called from the command line.

Don
-----
It seems to be using /bin/sh as the shell even thou I requested that it uses bash with the shebang. The log file shows that when I issue the command from the CLI it uses bash.
I included the passed variable before the fstrim command is issued and it seems to recognise the variable without a problem. It's only after the fstrim command that the variable is not recognised!

The OS I'm using is Debian derivative Kali.

Script used to generate logfile below.
Code:
#!/bin/bash
###############################################################################
##  SCRIPT TO TRIM SSD DRIVE
###############################################################################
##  INPUT: $1 MOUNT POINT OF PARTITION
###############################################################################
LOG=/TRIM.LOG
echo "####" >> $LOG
echo $(date) >> $LOG
echo "SHELL is $SHELL" >> $LOG

    if [ -z "$1" ];
    then echo "No Target" >> $LOG
    else echo "Trimming $1 $(fstrim -v $1)" >> $LOG
        fi

Logfile from above script. Last line shows output from command manually issued at the CLI.
Code:
####
Tue Jun 9 03:31:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:32:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:33:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:34:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:35:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:36:01 BST 2015
SHELL is /bin/sh
Trimming / 
####
Tue Jun 9 03:36:28 BST 2015
SHELL is /bin/bash
Trimming / /: 380747776 bytes were trimmed

Just to see what would happed, I added the eval command just before fstrim that had no effect. Also I tried coding the variable differently i.e. ${1} and ${$1} also no change.
Interestingly, I did find this site that seems to suggest either using the 'bash -c' command or setting the EV 'SHELL=/bin/bash' actually in the crontab file.
How to change cron shell (sh to bash)? - Unix & Linux Stack Exchange
I'll give this a go and report back, but I'd still like to permanently change the shell crontab uses to Bash.

Thanks for your help.

---------- Post updated 09-06-15 at 12:23 AM ---------- Previous update was 08-06-15 at 11:50 PM ----------

Followed the file /bin/sh and it lead to a Debian specific shell called dash!
Apparently it is supposed to be compatible with bash as documented here
Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh - Hyperpolyglot
using the same notation for passed parameters.

Doesn't seem to want to work with the EV set in the crontab file. Was going to change the /etc/crontab setting to SHELL=bin/bash but the whole system has been setup to run faster with dash as bash is much slower and who knows what changing shells will actually do.

If it is not the shell as it does seem to accept the variables that have been passed, could it be a problem with the fstrim command?

Last edited by ASGR; 06-09-2015 at 01:28 AM..
# 7  
Old 06-09-2015
As has been said MANY times before, cron doesn't create the same environment that you get when you login. And, there is nothing in your script that attempts to set variables that are needed in your script (in this case, cron is running your job with a default setting for PATH, and it appears that fstrim is not found on the default PATH) or fstrim needs something set in its environment that cron does not provide.

If you change:
Code:
    else echo "Trimming $1 $(fstrim -v $1)" >> $LOG

to:
Code:
    else echo "Trimming $1 $(/absolute/path/to/fstrim -v $1)" >> $LOG

where /absolute/path/to is the directory in which the fstrim utility is located on your system and fstrim doesn't depend on any environment variables being set to values not initialized as needed by cron, it might work. Otherwise, you need to set up your login environment in /etc/cron.hourly/trim using the initialization files your login shell invokes when you login, or explicitly determine exactly what is in your environment when your script works that is not in your environment when your script fails AND then be sure the needed environment is created in /etc/cron.hourly/trim before it invokes fstrim.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parameters in crontab

I need to write a shell script which send an alert if a particular script scheduled in cron is not triggered. My concern is that particular script which is scheduled in crontab, runs at night but it requires a file from upstream system as a feed prior to execution. it waits for that file upto 4... (2 Replies)
Discussion started by: Ankit Srivastav
2 Replies

2. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

3. Shell Programming and Scripting

Passing Parameters to Crontab

Hello Experts, I have a requirement to pass some parameters to Linux cron tab. For ex: My default cron entry looks like this as below: ------------------------------- 55 10 * * --... (7 Replies)
Discussion started by: MaheshChaudhari
7 Replies

4. UNIX for Dummies Questions & Answers

Perl Script:how to find how many parameters are required to run the script

How to find how many parameters are required to run a Perl script? (1 Reply)
Discussion started by: Lakshman_Gupta
1 Replies

5. Shell Programming and Scripting

ksh processing options and parameters

I have a requirement where I need to process both options and parameters. command line call ie xxx.ksh -sid TEST1 -search_str LOCKED user1 user2 ..... I am using the following peice of code but I am usure how I can loop through all my parameters user1, user2, ... Note at the minium... (2 Replies)
Discussion started by: BeefStu
2 Replies

6. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

7. Shell Programming and Scripting

want to pass parameters to awk script from shell script

Hello, I have this awk script that I want to execute by passing parameters through a shell script. I'm a little confused. This awk script removes duplicates from an input file. Ok, so I have a .sh file called rem_dups.sh #!/usr/bin/sh... (4 Replies)
Discussion started by: script_op2a
4 Replies

8. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

9. Shell Programming and Scripting

call a script from another script passing parameters

I want to call script2 from script1 passing parameters. I want to read the parameters list in script1, check the local directory (for example - lpath1|lpath2|lpath3|lpath4|lpath5|) for the existance of files and set the` lcount` to the number of files in this folder (`... (2 Replies)
Discussion started by: Lenora2009
2 Replies

10. Shell Programming and Scripting

help me in sending parameters from sqlplus script to unix shell script

Can anybody help me out in sending parameters from sql*plus script to unix shell script without using flat files.. Initially in a shell script i will call sql*plus and after getting some value from some tables, i want that variable value in unix shell script. How can i do this? Please tell me... (2 Replies)
Discussion started by: Hara
2 Replies
Login or Register to Ask a Question