Script format question

Posted on 2010-11-09
Last Modified: 2012-05-10
I am creating a script to write zero data to a CF card (Don't ask.  It's stupid but I have to do it anyway)  

sudo watch dd if=/dev/zero of=/dev/sdb bs=4k > mylog & tail mylog

My question is three parts.  

1)  Using the line above, why does the dd action stop?  If I take the " & tail mylog" off, it works and writes to the file just fine.

2)  Why can I not get this to run in a script from a desktop icon?

3)  Is there a better way to do this?  It has to be easy for the end user so the hope would be a double click on the desktop icon, password for sudo is fine and then the process runs with an easy to read status.

I have several more action icons to make after this one so hopefully this will get me on the right track.

Thanks all!
Question by:Helixx
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 79

Accepted Solution

arnold earned 250 total points
ID: 34097866
the problem is that you are trying to execute a non graphical command in a graphical environment:

your icon shortcut should be xterm -e sudo watch dd if=/dev/zero of=/dev/sdb bs=4k > mylog & tail mylog

What this will do is it will start an xterm window with instructions to run your command.

Author Comment

ID: 34098423
Okay.  I see where you are going with this but it still does not show the status.  Just a blank screen while the dd runs.  This is an Ubuntu machine if that makes any difference.
LVL 68

Assisted Solution

woolmilkporc earned 250 total points
ID: 34100147
xterm -e sudo watch dd if=/dev/zero of=/dev/sdb bs=4k | tee mylog

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 34101985
That didn't work either.  I just get a blank white background terminal screen after I type in the sudo password.  Once I type CTRL+C the screen disappears.  I do see dd running in another window using top.

I really like the look of pv and have been trying to get that to work.  So far I have this:

echo 'z,,,1234' | sudo -S dd if=/dev/zero bs=10M | pv -s 16G -N dd | echo 'z,,,1234' | sudo -S dd bs=10M of=/dev/sdb

It took me awhile to figure out that I would need to echo the password twice.  I get no error but it also doesn't do much.
administrator@kiosk1:~$ echo '123456' | sudo -S dd if=/dev/zero bs=10M | pv -s 16G -N dd | echo '123456' | sudo -S dd bs=10M of=/dev/sdb0+1 records in0+1 records out9 bytes (9 B) copied, 0.0048828 s, 1.8 kB/s       dd:    0B 0:00:00 [   0B/s ] [>                        ]  0%            administrator@kiosk1:~$

I think sudo may be my problem.  I remember a few version ago where you could sudo su and be done with it.  I know the dangers but this PC will not be on the network and will only be used by IT folks for a few specific tasks.
LVL 79

Expert Comment

ID: 34102959
you may need to escape some parameters
xterm -e "sudo watch dd if=/dev/zero of=/dev/sdb bs=4k \> mylog \& tail mylog"

I.e. the & was sending "xterm -e "sudo watch dd if=/dev/zero of=/dev/sdb bs=4k \> mylog" into the background and there is no interface to run the tail mylog.

Author Comment

ID: 34103130
Still get the blank screen so I can't see the progress.
LVL 79

Expert Comment

ID: 34103737
Is the mylog file gets created? This will at least deal with whether the program runs.

xterm -e "sudo watch dd if=/dev/zero of=/dev/sdb bs=4k \\> mylog \\& tail mylog"

You may have to add another escape if this does not work.
xterm -e "sudo watch dd if=/dev/zero of=/dev/sdb bs=4k \\\> mylog \\\& tail mylog"

Author Comment

ID: 34104786
Okay guys.  I got it working.  I really wanted to use pv as it has a much easier to read output but the sudo chaining was causing issues so this is what I ended up doing.  Also note, this takes the echo of the password out of the script so a little better security for when it matters.

  1. sudo passwd root // I wanted to get away from sudo and back to a su environment.
  2. xterm -e su -c 'bash /home/administrator/Desktop/ && bash'   // I
    added the su to the launcher.  Don't forget that the path will change so add back the full path to the script.
  3. below is the finished listed above.

echo "WARNING!! CF card will be wiped!"
echo "Press CTRL+C to stop this process now"

sleep 10

dd if=/dev/zero bs=10M | pv -s 16G -N dd | dd bs=10M of=/dev/sdb

echo "clean complete"

sleep 5

Thank you all for your help and I hope this helps someone else.

Author Closing Comment

ID: 34104818
I finally figured it out but not without the help of these guys.
LVL 79

Expert Comment

ID: 34107850
The alternative is that you can use suid to set the permission on the script to run with owner's rights.

i.e. root owned script with chmod 2755 thisscript.
The user would not need to know the su - password nor have rights to sudo.

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question