LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2015, 09:38 PM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Rep: Reputation: 31
Script to run fstrim and observe output


I can run "/usr/sbin/fstrim -v /" in a terminal as root and see the output.

I have tried to create a bash script that I can run manually and observe the output (to confirm fstrim ran). So far my script attempts fail to run the fstrim command, getting error messages like "fstrim: /: FITRIM ioctl failed: Operation not permitted". Can someone offer a suggestion?
 
Old 10-02-2015, 02:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show what you have written and how it is being executed and by whom (ie root or other)?
 
Old 10-02-2015, 04:44 PM   #3
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Since I posted my request, I have made some progress.

When I run the command in a terminal as root, I get:

Quote:
[root@localhost john]# /usr/sbin/fstrim -v /
/: 165.5 MiB (173498368 bytes) trimmed
[root@localhost john]#
Note terminal returns to ready for next command.


When I run the script below as root, a terminal opens, and after a delay prints an output (but does not advance to the 'ready for next command state':

Quote:
#!/bin/sh
set -x #echo on
konsole --noclose -e /usr/sbin/fstrim -v /

/: 165.5 MiB (173498368 bytes) trimmed
If I add an echo statement before the trim command to indicate the trim function (which can take some time) has started), I get only the echo, and not the trim:

Quote:
#!/bin/sh
set -x #echo on
konsole --noclose -e echo "Trim SSD"
/usr/sbin/fstrim -v /

Trim SSD
Ideally, I would like the script to open a terminal, print "Trim SSD", activate the trim command, print the output, return the terminal to the 'waiting for next command' state, but not close the terminal.
 
Old 10-03-2015, 05:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
From the look of it you will need to investigate konsole more thoroughly to make it do what you want. I am not familiar with it unfortunately.

I do however have a question, out of curiosity, why would you run a script, presumably from a terminal, only to open another terminal?
 
Old 10-03-2015, 06:56 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Thumbs up

I think you're confusing a terminal emulator (konsole,Xterm,rxvt,...) with a shell (sh,bash,csh,...)
A terminal emulator is just a frontend to allow you to use a CLI program (typically, a shell) inside a GUI desktop environment. It runs the CLI program for you, it sends whatever you type to the program and it displays its output in a window.
A shell is a program that actually interprets and executes the commands.


Quote:
Originally Posted by Johng View Post
When I run the script below as root, a terminal opens, and after a delay prints an output (but does not advance to the 'ready for next command state':
Code:
#!/bin/sh
set -x #echo on
konsole --noclose -e /usr/sbin/fstrim -v /

/: 165.5 MiB (173498368 bytes) trimmed
The --noclose argument tells konsole to remain open after the program being run within it (usually, the shell) exits. That is good if you want to examine the output before closing it, but nothing else. With the shell having exited, the konsole instance basically becomes a dead, non-interactive window. You can still type into it, but there is no program to recieve the data.

The -e fstrim ... switch tells konsole to run fstrim (with the arguments specified) as the program (that is, instead of your default shell, it runs fstrim). That is possible and works, but you cannot expect fstrim to interactively interpret shell commands.

So, what you seem to want is:
1/ the program run within konsole should be a shell.
2/ no --noclose option. You don't want konsole to remain open after the shell exits. You want the shell to not exit after it is finished.

Therefore, what you really should be studying is the man page of the shell, rather than the man page of konsole.

Using all the information above, something like this could work for you:

Code:
konsole -e sh -c "echo Trim SSD; /usr/sbin/fstrim -v /; sh"
However, I agree with grail. What you're trying to do seems a little weird. I wonder what you're actually trying to achieve in the first place. Perhaps you want to run this from cron? Or you intend to assign a keyboard shortcut for it?
 
Old 10-03-2015, 05:25 PM   #6
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
All I wanted to do, is simply click on a desktop icon to perform the SSD trim, and disply the output. If I create a desktop 'Link to Application' icon with the command you suggested (run as root), it performs as desired:

Quote:
Trim SSD
/: 30.8 GiB (33059778560 bytes) trimmed
sh-4.3#
Thank you.
 
Old 10-04-2015, 09:25 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED if happy with solution
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Would this be a valid script for fstrim in rc.6? tobyl Slackware 2 04-07-2015 11:58 AM
[SOLVED] parse file and use output to run a script johnthrax Programming 4 06-13-2013 09:40 AM
[SOLVED] redirecting output, observe with tail -f <file>, while running, without screen? srons Linux - Newbie 3 04-22-2010 06:43 AM
Correct output from script appears only when script is run interactively kaplan71 Linux - Software 2 01-15-2009 11:47 AM
awk run script on field before output Geneset Programming 3 08-26-2008 04:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration