How to redirect the messages from the script to console in Linux?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect the messages from the script to console in Linux?
# 1  
Old 11-12-2014
How to redirect the messages from the script to console in Linux?

Code:
msg.sh
#!/bin/bash
if [ -e /user/script/$start.sh]
then 
   echo "starting  service"
else
   echo " service not started" 
   echo " Please check the start.sh file or manuly start the service" 
fi

if i login with root credentials @12.36.34.123 with passwd
username:root
passwd:abc

once i login into linux pc on console the above messages should be availble like below: what coomand i need to use in linux to display the messages in consloe from the script ?

output:

:/ #

service not started
Please check the start.sh file or manuly start the service
# 2  
Old 11-12-2014
Quote:
Originally Posted by saku
Code:
msg.sh
#!/bin/bash
if [ -e /user/script/$start.sh]
then 
   echo "starting  service"
else
   echo " service not started" 
   echo " Please check the start.sh file or manuly start the service" 
fi

if i login with root credentials @12.36.34.123 with passwd
username:root
passwd:abc

once i login into linux pc on console the above messages should be availble like below: what coomand i need to use in linux to display the messages in consloe from the script ?

output:

Code:
:/ # 

service not started
Please check the start.sh file or manuly start the service

Didn't you get some output from bash like?:
Code:
bash: [: missing `]'

Where is the variable start defined? (Did you intend to say start.sh instead of $start.sh?)

There has to be a space between /user/script/$start.sh (or /user/script/start.sh) and ].
# 3  
Old 11-12-2014
yes , i am getting the output when i run my script : my script is working and it is giving the below output and cron job i sheduled every week.

output:

Code:
service not started
Please check the start.sh file or manuly start the service

the problem is once the script run's some time it will start service or it will tell service is not started.

my requirement is whenever i login that linux the output messages should be print in console. what command i need to redirect the output to console .
whenever i login the linux i want to see the output

Code:
service started

or

Code:
service not started
Please check the start.sh file or manuly start the service

---------- Post updated at 03:07 AM ---------- Previous update was at 01:27 AM ----------

how to get the outout from script to console.

i am running one script msg.sh using cron job every suday midnight. as soon as i logged in i want to see the staus is service started or service failed on console.

what command i need to add to script ?

msg.sh
Code:
#!/bin/bash
if [ -e /user/script/$start.sh]
then 
echo "starting service"
else
echo " service not started" 
echo " Please check the start.sh file or manuly start the service" 
fi

if i login with root credentials @12.36.34.123 with passwd
Code:
username:root
passwd:abc

i once i logged in linux service started or service failed ( out put of the script ) should be printed on console.

how to check or how to extract messages in linux from var/log/message to current console

Last edited by Don Cragun; 11-12-2014 at 01:54 PM.. Reason: Add CODE and ICODE tags again.
# 4  
Old 11-12-2014
Getting your act together BEFORE you post would definitely help us help you.
Trying to boil down what you said:
You have a script, verified it is well-behaved when started from the command line, run it from cron at Sunday midnight and want to see its output when logging in next time?
Per default, cron will mail a job's output to the respective user. So, you could check your mails at login and find the desired output.
If you want it displayed at login, have cron write a logfile, eventually to your HOME directory, and in your shell's startup/login script, use e.g. cat or more / less to output it to your terminal (which I guess is what you mean by "console").

What will show up in /var/log/messages related to your job will be sth like
Code:
cron[1150]: Job `cron.daily' started

unless you use e.g. logger in your script to explicitely log it to the system logs.
# 5  
Old 11-12-2014
Hi,

yes like shell startup/login script it should send the msg on console/terminal like , and i am using suse linux

service started

or

service not started
Please check the start.sh file or manuly start the service

Last edited by saku; 11-12-2014 at 06:02 AM.. Reason: to describe
# 6  
Old 11-12-2014
You are already doing 'send the msg on console/terminal'.
In which case, you could execute this 'msg.sh' script at login.
For a bash based linux, this could be in: $HOME/.bashrc (note the . before the name)

Or do you ment to write to the logs?
Which in case of a regular service should already be done - see journalctl (1) for more details.

Otherwise you could always redirect your output to a file, which you can read at any time later.
To overwrite existing content:
Code:
echo "Service started" > /path/to/output_file

To append:
Code:
echo "Service failed" >> /path/to/output_file

Hope this helps
# 7  
Old 11-12-2014
Please look at post #2 in this thread. You seem to have ignored everything I asked.

Change your script to the following and run it from the command line (not from cron):
Code:
#!/bin/bash
printf 'start is set to "%s"\n' "$start"
ls -l /user/script/$start.sh /user/script/start.sh
echo "running $0"
if [ -e /user/script/$start.sh ]
then 
echo "starting service"
else
echo " service not started" 
echo " Please check the start.sh file or manually start the service" 
fi

Note that I added the space in your [ command that I told you had to add before. Without that space, your syntax error guarantees that the then clause in your if statement will NEVER be executed.

Then notice that there is absolutely nothing in your script that will start anything! You check to see if a file exists (and, by convention, it looks like that file is a shell script), but if that file does exist, there is no attempt to execute it. If you were ever to execute the command:
Code:
echo "starting service"

it would be reporting a lie. This script never makes any attempt to start anything even after the syntax error is corrected!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux Console for Interactive Input Script

Hi there, How do I enter command to the interactive console. I was able to do it via the first line. What if I have more lines to input into the interactive console from line 3 onwards. Is there a more easier way? (echo -e "load openvas" ; echo -e "openvas_help") | msfconsole ... (4 Replies)
Discussion started by: alvinoo
4 Replies

2. Linux

Linux extacting msg from script to console

how to get the outout from script to console. i am running one script msg.sh using cron job every suday midnight. as soon as i logged in i want to see the staus is service started or service failed on console. what command i need to add to script ? msg.sh #!/bin/bash if then echo... (1 Reply)
Discussion started by: saku
1 Replies

3. AIX

Console redirect to xmanager in AIX ?

Hello, Oracle PDF reports run only when you run the command # xhost + from the console, but if you run from the xmanager session , then report doesn't work running from windows pc using xmanager # xhost + access control disabled, clients can connect from any host # echo... (7 Replies)
Discussion started by: filosophizer
7 Replies

4. Shell Programming and Scripting

Redirect an output from a script to a file and display it at a console simultaneously

Hi, I'd like to redirect the STDOUT output from my script to a file and simultaneously display it at a console. I've tried this command: myscript.sh | tail -f However, it doesn't end after the script finishes running I've also tried this: myscript.sh | tee ~/results.txt But it writes... (3 Replies)
Discussion started by: wenclu
3 Replies

5. Shell Programming and Scripting

How to redirect the output of a cvs command to a file as well as the console.

Hi can anyone tell me how to redirect the ouput of a cvs command to a file as well as the console? i tried using cvs add <filename> | tee logFile cvs add <filename> 2>logFile 2>&1 All i could get is only on console or on file. Please help Thanks (2 Replies)
Discussion started by: ankitag2010
2 Replies

6. UNIX for Dummies Questions & Answers

Windows to Linux remote console using VNC brings up blank console screen with only mouse pointer

:confused:Hi This was installed on the Linux box a few weeks back by a guy that no longer works for us. All worked fine until last week. Now when we connect its just a blank screen with no icons. I get a whole bunch of errors when starting the service too: Tue Feb 23 14:29:45 2010 ... (1 Reply)
Discussion started by: wbdevilliers
1 Replies

7. Solaris

Command to redirect console to my tty?

Is there a utility built into Solaris that will allow me to see console messages from a tty? I've done a search and see that this is possible through software like ILOM, but I'm looking for a method to do this with built in utilities. For example, on AIX, I can use swcons `tty` (6 Replies)
Discussion started by: makodarear
6 Replies

8. Shell Programming and Scripting

How to redirect the output to multiple files without putting on console

How to redirect the output to multiple files without putting on console I tried tee but it writes to STDOUT , which I do not want. Test.sh ------------------ #!/bin/ksh echo "Hello " tee -a file1 file2 ---------------------------- $>./Test.sh $> Expected output: -------------------... (2 Replies)
Discussion started by: prashant43
2 Replies

9. Solaris

console redirect on dl365

anyone have any success with redirecting the console running sol10 x86 on a hp dl365? (0 Replies)
Discussion started by: pupp
0 Replies

10. Shell Programming and Scripting

console redirect of progress bar

I'm trying to write a python wrapper around wget to show the progress bar on a gui application. Wget gives you a progress bar on command line, but how would I pipe that to some "tty" that's really just a variable I read, or am I going about it all wrong? When I try to just redirect the output to a... (1 Reply)
Discussion started by: unclecameron
1 Replies
Login or Register to Ask a Question