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:
And the script is:
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.
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?
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:
includes the argument passed to your script by cron when it writes to the log file.
If you add the line:
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:
What operating system are you using?
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.
Logfile from above script. Last line shows output from command manually issued at the CLI.
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?
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:
to:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)