2

I believe I have everything setup correctly for my if else statement however it keeps outputting content into my shell terminal as if i ran the command myself. is there anyway i can escape this so i can run these commands without it populating my terminal with text from the results?

#!/bin/bash
ps cax | grep python > /dev/null
if [ $? -eq 0 ]; then
  echo "Process is running." &
  echo $!
else
  echo "Process is not running... Starting..."
  python likebot.py &
  echo $!
fi

Here is what the output looks like a few minutes after running my bash script

[~]# sh check.sh
Process is not running... Starting...
12359
[~]# Your account has been rated. Sleeping on kranze for 1 minute(s). Liked 0 photo(s)...
Your account has been rated. Sleeping on kranze for 2 minute(s). Liked 0 photo(s)...
2
  • What in the output don't you want to see anymore? Commented Aug 20, 2013 at 12:23
  • Just a question: What is the behaviour you are expecting here echo "Process is running." & echo $! Commented Aug 20, 2013 at 14:02

5 Answers 5

3

If you want to redirect output from within the shell script, you use exec:

exec 1>/dev/null 2>&1

This will redirect everything from now on. If you want to output to a log:

exec 1>/tmp/logfile 2>&1

To append a log:

exec 1>>/tmp/logfile 2>&1

To backup your handles so you can restore them:

exec 3>&1 4>&2
exec 1>/dev/null 2>&1

# Do some stuff

# Restore descriptors
exec 1>&3 2>&4

# Close the descriptors.
exec 3>&- 4>&-

If there is a particular section of a script you want to silence:

#!/bin/bash

echo Hey, check me out, I can make noise!

{
    echo Thats not fair, I am being silenced!
    mv -v /tmp/a /tmp/b

    echo Me too.
} 1>/dev/null 2>&1
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to redirect the "normal (stdout)" output use >/dev/null if you also want to redirect the error output as well use 2>&1 >/dev/null

eg

$ command 2>&1 >/dev/null

Comments

2

I think you have to redirect STDOUT (and may be STDERR) of the python interpreter:

...
  echo "Process is not running... Starting..."
  python likebot.py >/dev/null 2>&1 &
...

For further details, please have a look at Bash IO-Redirection.

Hope that helped a bit. *Jost

Comments

1

You have two options:

  • You can redirect standard output to a log file using > /path/to/file
  • You can redirect standard output to /dev/null to get rid of it completely using > /dev/null

If you want error output redirected as well use &>

See here

Also, not relevant to this particular example, but some bash commands support a 'quiet' or 'silent' flag.

Comments

0

Append >> /path/to/outputfile/outputfile.txt to the end of every echo statement

echo "Process is running." >> /path/to/outputfile/outputfile.txt

Alternatively, send the output to the file when you run the script from the shell

 [~]# sh check.sh >> /path/to/outputfile/outputfile.txt

4 Comments

You don't need to do that.
@Craig You do propose a better approach-which you hadn't at the time I posted. What I suggested does work, your approach is merely better
@kakoma Your "solution" "works" in some situations, but it can also induce more bugs (like not having correct permissions etc). There are bad solutions and there are proper ones, yours is the former.
@Jite I believe my comment acknowledges that. Thanks Craig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.