I wrote a shell script that I use under ash, and I redirect stderr and stdout to a log file. I would like that log file to be emailed to me only if stderr is not empty.
I tried:
exec >mylog.log 2>&1
# Perform various find commands
if [TEST_IF_STDERR_NOT_EMPTY]; then
/usr/bin/mail -s "mylog" email#mydomain.com < mylog.log
fi
My question is twofold:
1- I get a -sh: /usr/bin/mail: not found error. It seems that the mail command doesn't exist under ash (or at least under my linux box, which is a Synology NAS), what would be the alternative? Worst case, perl is available, but I would prefer to use standard sh commands.
2- How to I test that stderr is not empty?
Thanks
How to check if file is empty in bash
As for the first question, in your code you are calling mail but lower in the post you are calling email. Check your code and make sure it is mail.
Use which mail to get the full path. Maybe it is not installed in /usr/bin/.
Use find to locate mail.
If you can go to another shell, run it and then execute which mail to get the full path of mail in case the path is set up in the alternative shells.
Related
I am having a strange issue. I have written a script which is basically running a perl script in remote server using ssh.
This script is working fine but after completion of the above operation it will ask user to choose the next operation.
it is showing the options in the command prompt but while I am giving any input it is not showing in the screen even after hitting enter also it remain same.
I am not getting what is the exact issue, but it seems there is some issue with the ssh command because if I am commenting out the ssh command it is working fine.
OPERATION:
print "1: run the script in remote server \n2: Exit\n\nEnter your choice:";
my $input=<STDIN>;
chomp($input);
..........
sub run_script()
{
my $com="sshg3.exe server -q --user=user --password=pass -exec script >/dev/null";
system("$com");
goto OPERATION;
}
after completing this ssh script it is showing in screen:
1: run remote script
2: exit
Enter your choice:
but while I am giving any input it is not displaying in the screen until and unless I am exiting it using crtl C.
Please can anyone help what might be the issue here ?
One of the classic gotchas with ssh is this - that it normally runs interactively, and as such will attach STDIN by default.
This can result in STDIN being consumed by ssh rather than your script.
Try it with ssh -n instead.
You can redirect the output in command prompt if -n option is not available for you.
try this one it might work for you.
system("$com />null");
As per https://support.ssh.com/manuals/client-user/62/sshg3.html there is an option for redirecting input use --dev-null (*nix) or --null (Windows).
-n, --dev-null (Unix), -n, --null (Windows)
Redirects input from /dev/null (Unix) and from NUL (Windows).
I have a perl file (eg:test.pl) which does some DB operations.
While testing, its working fine.
I execute this file as a background process by using the command
perl test.pl &
Its working properly for some days.
But after some days ,the file execution get stopped.
How can I find the reason or view the error?
I checked the log file "/var/log/httpd/error_log", but can't find anything.
I keep the perl file in a server, which runs in Cent OS.
Any one have idea?
There is no 'perl error log'
But you can define a destination for output to be saved to, just run your script like this:
perl test.pl >> /var/log/some-log-file.log 2>&1 &
This will redirect STDOUT (normal shell output) and STDERR (error output) to /var/log/some-log-file.log instead of to the terminal.
You may also wish to use nohup in order to have the script ignore HANGUP (logout) signals, which could be causing your unexpected terminations:
nohup perl test.pl >> /var/log/some-log-file.log 2>&1 &
Obviously, whichever user you run the script as will need to have write access to the log file.
I have the following excerpt from a perl script to automate an FTP session, I'm hoping someone can explain how it works.
system("rsh some_server ftp -in ftp.something.com << !
user anonymous someone\#somewhere.org
some ftp commands
bye");
The background. This perl script runs on a Linux machine, it remotes into a Solaris machine. The FTP session must be executed from the Solaris machine because the FTP site performs IP address checking.
Formerly this script ran on the Solaris machine directly (i.e. it didn't use rsh) I hacked it around and came up with this which seems to work. However I have little idea how, in particular I don't understand the << ! bit at the end of the first line. It looks a little like a here-document but I'm not really sure.
Any explanations welcome.
You are right, << is a heredoc, which is made clear by the following warning (which I get when I take out the rsh command):
sh: line 2: warning: here-document at line 0 delimited by end-of-file (wanted `!')
The construct
<< HEREDOC
reads as standard input everything from HEREDOC up to a line containing only HEREDOC or up to an end-of-file character. When you put this after a command, it is equivalent to
command < file
where file contains the text in the heredoc. In your case, instead of HEREDOC the delimiter is !, so the ! is not passed to ftp but everything after ! is. This is equivalent to
$ cat file
user anonymous someone\#somewhere.org
some ftp commands
bye
$ ftp -in ftp.something.com < file
rsh takes that entire command and runs it on your remote host.
As illustrated by user1146334's answer, this command does not act on the principal of least surprise. At the very least, make it less confusing by changing it to
system("rsh some_server ftp -in ftp.something.com << HEREDOC
user anonymous someone\#somewhere.org
some ftp commands
bye
HEREDOC");
Or even better, as mpapec mentioned in the comments, use Net::FTP and Net::SSH2.
Did you look at the man page?
-i Turns off interactive prompting during multiple file transfers.
-n Restrains ftp from attempting “auto-login” upon initial connection. If auto-login is enabled, ftp will check the .netrc (see netrc(5)) file in the user's
home directory for an entry describing an account on the remote machine. If no entry exists, ftp will prompt for the remote machine login name (default is
the user identity on the local machine), and, if necessary, prompt for a password and an account with which to login.
The client host and an optional port number with which ftp is to communicate may be specified on the command line. If this is done, ftp will immediately attempt
to establish a connection to an FTP server on that host; otherwise, ftp will enter its command interpreter and await instructions from the user. When ftp is
awaiting commands from the user the prompt ‘ftp>’ is provided to the user. The following commands are recognized by ftp:
! [command [args]]
Invoke an interactive shell on the local machine. If there are arguments, the first is taken to be a command to execute directly, with the rest of
the arguments as its arguments.
So essentially you're ftp'ing in and providing a new command per line in-line instead of from a file.
I want to do the opposite of everybody ( laugh )
I start open-erp with a command line like :
C:\OpenERPAllInOne\Server>openerp-server.exe --log-file=outputfile
but the problem is that with Windows it only does output to a file.
Is there any way to redirect a file to the STDOUT.
For example (it doesn't work but this is the way I see it working) :
C:\OpenERPAllInOne\Server>openerp-server.exe --log-file=STDOUT
and then see directly the output to the command line.
I can't make it work ! Any idea ?
Thanks,
Olivier
I'm not sure if it still works, try CON (hope it was that) as filename
That would be a function of the openerp-server.exe program.
If it's default output is to a file, then there isn't anything you can do about that.
I'm assuming that OpenERP uses getopt_long and confirms to GNU standards. If it does, then the following should work:
openerp-server --log-file=-
(a dash is usually used to represent STDOUT in GNU, Linux and UNIX programs.)
If you can't get the server to output to the standard output you could monitor the log file with [tail][1].
If you run:
tail -f c:\path\to\logfile.txt
in another window then you can see the contents of the file as they are written.
tail doesn't come with Windows as standard but you can download a free port of tail for Windows here.
for windows there is a configuration file for OpenERP Server,
like openerp-server.conf in the directory where you have installed the Open ERP
like c:\Program Files\OpenERP Server\openerp-server.conf OR
C:\documents and settings\USER\OpenERP Server\oepenrp-server.conf
you need to edit that file and set the --log-file paramter to None or False, and probably you might also try out something like this
C:\OpenERPAllInOne\Server>openerp-server.exe --log-file=False
that should work, but not sure whether it will work or not.!!
I am using sendmail in perl and noticed (after much banging of head against wall) that when the script is run at the command line it needs you to leave out the \n(s) after your e-mail and the recipient's email address in order to format the mail correctly, but when running via CGI if those \n(s) aren't there it returns an error stating that the recipient's e-mail is malformed.
Has anyone else encountered this? What are the two doing differently?
I am betting that you are getting data from prompts in on the commandline and not chomping them like this:
my $send_to = <>;
This means $send_to will already have a "\n". To make them both work the same way chomp the variables:
my $send_to = <>;
chomp($send_to);
or just
chomp(my $send_to = <>);
In a couple of your comments you mention that you're running the script from the command line with the -l option (perl -l foo.cgi).
The -l option enables automatic line-ending processing, and as your problem is with line endings, I suggest you try it without the -l.
Where is the data coming from? Hard coded in the script, or from a web form?
Just as an aside, if you get the recipient's email address from a web form, your form will be used by spammers. It's a 100% guarantee.
The term "CGI" is broad, if you mean your perl script run as a CGI versus yur perlscript run at the command line, I would look toward the pathing that the script has and its general inherited environment. Especially if your running it as different userids. If the webserver is in a chroot, etc.
use Data::Dumper;
warn(Dumper(\%ENV));
So I'm guessing that you have something like this for running it via the command line:
my $your_email = "you#foo.bar";
my $recipient_email = "them#foo.bar";
and this when "running via CGI":
my $your_email = "you#foo.bar\n";
my $recipient_email = "them#foo.bar\n";
So the question I would ask you then is how you're calling sendmail with the above variables, and also what you mean when you say "running via CGI" versus running via the command line? Are you just adding CGI code and still running via the command line or by visiting its URL in a web browser?