LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2017, 08:48 AM   #1
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Rep: Reputation: Disabled
why file is getting empty when script is running through crontab


Hello all ,

Below is my script i have passed my password in script itself. when i am running this script manually it is running good(got file on mail) but when i am running through crontab i am getting empty file on mail. could you some one tell me what can i do to resolve issue. when i am running script it is asking password but after 3 seconds it is taking password by itself.


#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
 
Old 05-16-2017, 09:03 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
after 3 seconds it might just be trying to or is running it without your pass word therefore coming up empty. If it is on your personal PC and no one can get to your script but you. adding your pass word to it maybe what is needed to be done if ran in auto mode and a password has to be used to get it to do what you want it to do.
 
Old 05-16-2017, 09:15 AM   #3
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you sir for very quick reply, i dint get exactly what i should do here for script working properly.yes it is right ,iy is running it without your pass word therefore coming up empty. what solution i can do here . please suggest. is there any different way to provide password in script itself
 
Old 05-16-2017, 09:22 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,102

Rep: Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450
Quote:
Originally Posted by Vpawar View Post
Hello all ,
Below is my script i have passed my password in script itself. when i am running this script manually it is running good(got file on mail) but when i am running through crontab i am getting empty file on mail. could you some one tell me what can i do to resolve issue. when i am running script it is asking password but after 3 seconds it is taking password by itself.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
Please use CODE tags when posting scripts.

I've bolded a line in the script above. When you run the script manually, you're running it as your logged-in user, so when you type in "cd", it returns to your home directory. When you run from CRON, it is NOT running as your user ID, but rather under the cron daemon. Put an entire path in the cd line, and also fully qualify the rm line with a complete path.

Aside from that, when you're running sudo, it gets the UID from the logged in session...manually, it's you. Through CRON?? It has no idea....read the man page on the sudo command, and pay particular attention to the -U flag. But is this cron job in your users crontab, or in the root crontab??? If it's in root....why use sudo at all, since that means the commands will run as root anyway, and not NEED sudo at all.

Last edited by TB0ne; 05-16-2017 at 09:26 AM.
 
2 members found this post helpful.
Old 05-16-2017, 10:17 AM   #5
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
 
Old 05-16-2017, 10:23 AM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,131

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Quote:
Originally Posted by TB0ne View Post
Put an entire path in the cd line, and also fully qualify the rm line with a complete path.
To second that comment: As a Best Practice, when developing scripts, ALWAYS use absolute paths to other scripts, commands and files.
example: use
Code:
/bin/sort [which sort], not just sort
/bin/mailx intead of mailx 
/home/vpawar/sasdevweekly.doc instead of cd and sasdevweekly.doc
I don't want to admit the number of times I've struggled with a script not working because it didn't know where find or ls was...
--
Sean
 
1 members found this post helpful.
Old 05-16-2017, 10:24 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Quote:
Originally Posted by Vpawar View Post
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
I think answering @TB0ne Questions might help him help you a LOT.
 
1 members found this post helpful.
Old 05-16-2017, 10:28 AM   #8
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
Seems to be a lot of inconsistency here over where sasdevweekly.doc is located.

Code:
cd /sasdev
puts you in /sasdev

Code:
> /home/vpawar/sasdevweekly.doc
overwrites/creates /home/vpawar/sasdevweekly.doc

Code:
-a sasdevweekly.doc
is looking for /sasdev/sasdevweekly.doc

Code:
rm sasdevweekly.doc
is looking to delete /sasdev/sasdevweekly.doc
 
1 members found this post helpful.
Old 05-16-2017, 10:30 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,102

Rep: Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450Reputation: 6450
Quote:
Originally Posted by Vpawar View Post
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
Answer the questions asked, and we may be able to help. Read the man page on the sudo command. And read the "Question Guidelines" link in my posting signature...unless you ask a clear question (and this post is NOT clear), we can't help.

And saying that you don't have root access when you DO have sudo access is very odd. You do realize that sudo is used to give 'regular' users root-level access, right??? You do know what sudo does?
 
Old 05-16-2017, 11:51 AM   #10
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
sir the problem is i cant create any file in
Quote:
/home/vpawar/sasdev
that is why i created file on local folder
Quote:
/home/vpawar/sasdevweekly.doc
second thing is when i am using super user do (sudo ) system is asking me password, That is why i passed password in script itself.
i used absolute path where you guys have suggested but it is not heading to resolution of my problem.
when i am running it manually it is successfully doing it. is not running from my user id it is running through sudo access so it is asking me password . but through crontab it is sending empty file to me.
 
Old 05-16-2017, 12:39 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Quote:
Originally Posted by Vpawar View Post
sir the problem is i cant create any file in

that is why i created file on local folder

second thing is when i am using super user do (sudo ) system is asking me password, That is why i passed password in script itself.
i used absolute path where you guys have suggested but it is not heading to resolution of my problem.
when i am running it manually it is successfully doing it. is not running from my user id it is running through sudo access so it is asking me password . but through crontab it is sending empty file to me.
1. you never gave an answer to which side you are running it on, user or root.
if everything is on the root side then run it on root side. su password then set it up in cron.
or side step the password by changing your sudoers file to NOPASSWD for sudo group.

that is what I'd do and I do not even use cron. (well one file)
 
Old 05-16-2017, 12:53 PM   #12
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
I am running on user side.
 
Old 05-16-2017, 01:07 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Yours
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
why not do this on root side? My rewrite.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
#cd /sasdev <-- I assume that is a dir to that file below. if that is a file. 
#why not just do it like this?

echo /sasdev/Bajrang!1 | /usr/bin/du /sasdev -hsbx *|sort -rh > /tmp/sasdevweekly.doc
 
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a 
/tmp/sasdevweekly.doc vpawar@gmail.com

rm /tmp/sasdevweekly.doc
root can do whatever it wants - so doing this on root side to me seems more logical.

I have no idea if that rewrite works but theoretically it should.

Last edited by BW-userx; 05-16-2017 at 01:15 PM.
 
2 members found this post helpful.
Old 05-17-2017, 05:52 AM   #14
Vpawar
LQ Newbie
 
Registered: May 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you sir . I really appreciate your try for resolve this, let me give you explanation for every line
Quote:
cd /sasdev
I am getting in SASDEV volumn, because i need to run below commands on SASDEV volumn.
Quote:
echo passwrd!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
In above line i am passing passwrd!1 as a password because when i am executing only
Quote:
sudo /usr/bin/du /sasdev -hsbx *|sort -rh
this commands, system is asking me password so i am passing it in script itself and taking that output in sasdevweekly.doc
Note: i cant create file on SASDEV volumn, that is why i am creating file on my local folder(/home/vpawar).
Quote:
cd
getting in my local folder.
Quote:
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
here, i am sending sasdevweekly.doc file on my mailbox

when i am running this script manually it is taking password from script itself and sending out file successfully,but when i put it on cron it is not able to take that password and sending empty file.
 
Old 05-17-2017, 08:36 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
I'd get rid of that password needed problem by and if necessary set root up with a new/different mail account that root use. If that is needed for the report to get emailed out.

Because I do not see where it matters who sends it. A user or root user, as long as it gets sent.


This way being that /sasdev is indicating that it is on the root side of the system. Then Logically it should be Root user should be taking care of business. Just remove the sudo part. I do think your system should allow a sudo user to log in a terminal as su or su - to gain root privileges. Then set up your CRON job under root user, so that CRON will see it as root requesting this job to be ran. No password needed. that has now been eliminated. easy peasy. done.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report

/usr/bin/du /sasdev -hsbx *|sort -rh > /tmp/sasdevweekly.doc
 
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a 
/tmp/sasdevweekly.doc vpawar@gmail.com

rm /tmp/sasdevweekly.doc
Like @TB0ne questioned. Is this being ran on user side or Root side? Bang Really Big went off in my head, and I do not use CRON. Just applied methodology on how to get things done on the root side of the system.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] crontab not running script coralfang Linux - Software 1 09-10-2015 10:10 AM
Script is not running in Crontab Deepesh_tr Linux - Newbie 8 09-02-2012 07:43 PM
[SOLVED] Crontab Script Not Running th1bill Ubuntu 4 12-10-2010 03:49 PM
crontab not running script sunlinux Linux - Newbie 5 05-18-2010 06:21 AM
Running a script with crontab. glore2002 Slackware 3 06-05-2008 08:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:20 AM.

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