LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2015, 08:45 PM   #1
epower
LQ Newbie
 
Registered: Apr 2015
Posts: 4

Rep: Reputation: Disabled
Bash function in background


I am hoping someone can help me out here.

I have a function which acts as a job submission that is automatically sent as a background job. I would like the return of the job to represent what a user actually type in a command line but as of now it returns the literal function.

For example:

function hello() { echo "Hello $1" & }

when I issue the command:hello MyName

at completion I get

[1]+ Done echo "Hello $1"

What I would like to see is

[1]+ Done hello MyName


without having to issue a command

hello MyName &


Thank you for your help!
 
Old 04-09-2015, 11:56 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
That's because a background job can't get input and post output. Read man page of bash.
 
Old 04-10-2015, 12:04 AM   #3
epower
LQ Newbie
 
Registered: Apr 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
When a background job finishes.... you get a response back which includes the command you issue

if I have the following function

function hello() { echo "Hello $1" & ;}

and I issue the command

hello MyName &

I get: [1]+ Done hello MyName

what I would like to do is put this behavior into the function
 
Old 04-10-2015, 12:06 AM   #4
epower
LQ Newbie
 
Registered: Apr 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
sorry typo before

function hello() { echo "Hello $1" ;}

Last edited by epower; 04-10-2015 at 12:07 AM.
 
Old 04-10-2015, 12:15 AM   #5
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
function hello() { echo "Hello $1" ;}
Do you want to make this function executable in sub processes?

If then pass:

Code:
function hello() { echo "Hello $1" ;}
export -f hello
 
Old 04-10-2015, 12:29 AM   #6
epower
LQ Newbie
 
Registered: Apr 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
So the whole background on this is that our users kick off certain jobs and continue to work. These happen to be jobs for SAS which may take varying lengths.

so they submit a job through a command like sas program.sas and their job is submitted and they continue to do their other work.
In the process they may submit many jobs but when a job is done they are going to be prompted. The users would like to see which job finished.

so I have a function that looks similar to this
function sas () { /opt/local/bin/sas94 "$@" &}

they submit
sas program1.sas
sas program2.sas

As soon as the first program is done X minutes later they get this:

[1]+ Done /opt/local/bin/sas94 "$@"

This isnt informative of what just finished


If they were to do

sas program1.sas &
sas program2.sas &

they get assuming the first one finished, giving more meaningful information

[1]+ Done /opt/local/bin/sas94 program1.sas

So I am trying to figure out what I am missing in my function so they don't always need to add & to push a job in background and get this result.

In the past we had another environment csh where I could just do this in an alias and I would get the proper return. In bash it seems I need to get into a function to be able to pass the program parameter and im kind of stuck. Sorry I am really a SAS programmer not so much a Linux guy...

Last edited by epower; 04-10-2015 at 12:31 AM.
 
Old 04-10-2015, 12:54 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Probably this:
Code:
function sas {
     /opt/local/bin/sas94 "$@"
    echo sas94 "$@" completed
}

sas something &
?
 
Old 04-10-2015, 07:38 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
sas_core () {
    sleep 1
    printf 'Starting %s\n' "$1"
    sleep 5                         ### main process here
    printf 'Done %s\n' "$1"
    sleep 1
}

sas () { 
    (set +m; sas_core "$1" &) 
}

$ sas longoperation
$ Starting longoperation
Done longoperation

Last edited by NevemTeve; 04-10-2015 at 07:45 AM.
 
  


Reply



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] running function in background and exit main script... masavini Programming 2 05-20-2012 03:42 PM
How to run a function in background in c nathan Programming 11 08-28-2009 12:01 PM
run c function in the background schneidz Programming 4 09-07-2007 07:29 AM
run c function in the background schneidz Programming 1 09-06-2007 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

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