Jump to content
OMRON Forums

how are C Background tasks started?


vanandel

Recommended Posts

I would like to start my C Background task and pipe its output through another program that adds timestamps.

 

How can I modify the runtime environment for a C background task to pass a debug argument and to pipe its output through another program?

Link to comment
Share on other sites

  • Replies 16
  • Created
  • Last Reply

Top Posters In This Topic

Hi,

 

This can be done with the Linux "pipe" command, which uses this symbol: |

 

You just put the | symbol after your "output" program and before your "input" program when calling them on the same line.

 

For example, let's consider two very simple background programs.

 

Program 1 - the "out" program that outputs a string:

#include    // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"

int main(void) 
{
InitLibrary();
printf("Message_out\n"); // Print arbitrary string to stdout
CloseLibrary();	
return 0;
}

 

Program 2 - the "in" program that reads the string from the "out" program:

#include    // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"

int main(void) 
{
char str[128]="";
InitLibrary();
scanf("%s",str); // Read stdin
Send(1,str); // Print result to SEND1 port on Power PMAC unsolicited response port
CloseLibrary();	
return 0;
}

 

After building and downloading these, you can issue the following command from anywhere in Script (e.g. Terminal Window, PLC program, Motion Program) to start the "out" program and dump its stdout into the "in" program (note that when using the "system" command, you have to list the full paths of both the "output" program and the "input" program separated by the pipe symbol |):

 

system /var/ftp/usrflash/Project/C\ Language/Background\ Programs/out.out|/var/ftp/usrflash/Project/C\ Language/Background\ Programs/in.out

 

Or just simply the following if you're connected to PPMAC via a Telnet terminal or the like (here, we're doing everything from the same local path, so you do not need to write the full path for both out and in programs):

/var/ftp/usrflash/Project/C\ Language/Background\ Programs/out.out | in.out

Link to comment
Share on other sites

Perhaps my question was not clear. I know how to run commands on a command line with a '|' (pipe). How do I configure the PowerPMAC to run

 

/var/ftp/usrflash/Project/C\ Language/Background\ Programs/capp1.out | my_filter /var/log/capp1/capp1.log

 

automatically, when the system boots?

Link to comment
Share on other sites

Hi,

 

I accidentally deleted your most recent post, vanandel, but here's my response:

 

It seems to work for me, but try putting this into pp_startup.txt in case you still can't get it to work:

 

cpx system "/var/ftp/usrflash/Project/C\ Language/Background\ Programs/capp1.out | /usr/local/bin/timestamper /var/log/capp1/capp1.log"

 

Putting the "cpx" in front forces the command to be executed as though it's running in a program. Please let me know if that works for you.

Link to comment
Share on other sites

  • 7 months later...

Hi all

 

I'd like to run my C software in background and log the output (the printf output) on a text file.

The shell command should be:

/usr/local/bin/ads/capp1.out &> /var/log/ADS.txt 

 

I tried to use the system and cpx system commands, but I constanly get an error message; it seems it doesn't like the string with the ampersand character; if I issue the command in the terminal window, I get this error:

 

sh: -c: line 0: unexpected EOF while looking for matching `"'

 

What should I do to run the sw?

 

ciao

gg

Link to comment
Share on other sites

Hi all

 

I'd like to run my C software in background and log the output (the printf output) on a text file.

The shell command should be:

/usr/local/bin/ads/capp1.out &> /var/log/ADS.txt 

 

I tried to use the system and cpx system commands, but I constanly get an error message; it seems it doesn't like the string with the ampersand character; if I issue the command in the terminal window, I get this error:

 

sh: -c: line 0: unexpected EOF while looking for matching `"'

 

What should I do to run the sw?

 

ciao

gg

Link to comment
Share on other sites

  • 2 weeks later...

there are a couple of interesting fact on the system command behavior. So, the real command to send to the linux is:

system nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &

 

the syntax, without quote, works in a strange way: the terminal of the IDE does not responds anymore to commands, unless I hit CTRL+C. Then, it disconnects and reconnect, and I have my capp1.out running in background and the IDE responding to command.

If I use the system command with the quotes " , it still reply me with the error message

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

 

What am I doing wrong? How can I make my software run in background at project startup?

Link to comment
Share on other sites

there are a couple of interesting fact on the system command behavior. So, the real command to send to the linux is:

system nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &

 

the syntax, without quote, works in a strange way: the terminal of the IDE does not responds anymore to commands, unless I hit CTRL+C. Then, it disconnects and reconnect, and I have my capp1.out running in background and the IDE responding to command.

If I use the system command with the quotes " , it still reply me with the error message

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

 

What am I doing wrong? How can I make my software run in background at project startup?

Link to comment
Share on other sites

Hi,

 

The behavior you describe is not strange, actually; it's the expected behavior. When you send the command in the Terminal without quotes, you're telling Linux that you want to run that command directly in the Terminal's communication thread, so it naturally takes over the thread's stdout as long as the command is executing.

 

If you want to run that command at startup, you need to put it into a background PLC that gets called in pp_startup.txt, which is in the Configuration folder of the Solution Explorer.

 

For example, if you used PLC 1, you could put this into pp_startup.txt:

 

enable plc 1

 

Then, in PLC 1, you could put

 

open plc 1

system "nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &"

disable plc 1

close

 

I just tested this and it works.

Link to comment
Share on other sites

Hi,

 

The behavior you describe is not strange, actually; it's the expected behavior. When you send the command in the Terminal without quotes, you're telling Linux that you want to run that command directly in the Terminal's communication thread, so it naturally takes over the thread's stdout as long as the command is executing.

 

If you want to run that command at startup, you need to put it into a background PLC that gets called in pp_startup.txt, which is in the Configuration folder of the Solution Explorer.

 

For example, if you used PLC 1, you could put this into pp_startup.txt:

 

enable plc 1

 

Then, in PLC 1, you could put

 

open plc 1

system "nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &"

disable plc 1

close

 

I just tested this and it works.

Link to comment
Share on other sites

Hi Charles

 

The behavior you describe is not strange, actually; it's the expected behavior. When you send the command in the Terminal without quotes, you're telling Linux that you want to run that command directly in the Terminal's communication thread, so it naturally takes over the thread's stdout as long as the command is executing.

 

understood, thanks.

 

 

system "nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &"

disable plc 1

close

 

ok; I did it, and we moved the problem in another point: I can get the capp1 running, but I got an error now, that I can read from the log file.

The PLC system call cannot detect the correct LD_LIBRARY_PATH.

 

I added my full needed environmental variable LD_LIBRARY_PATH on the /etc/profile file; the correct value of my var is:

/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib

 

If I echo the variable from a shell (echo $LD_LIBRARY_PATH), I got the correct value.

If I echo the variable from the IDE terminal (system "echo $LD_LIBRARY_PATH"), I still got the correct value.

But, if I echo the variable from the PLC1 (system "echo $LD_LIBRARY_PATH > /var/log/ADSpath"), my LD_LIBRARY_PATH is empty (like it was before I modded the /etc/profile file).

I try to add on the PLC the instruction to export the variable (system "export LD_LIBRARY_PATH=\"/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib\" "), but it seems it does not like the command, and the echo report again an empty LD_LIBRARY_PATH var.

 

Maybe is the PLC is logged differently from the terminal or the common shell?

 

thanks a lot

gigi

Link to comment
Share on other sites

Hi Charles

 

The behavior you describe is not strange, actually; it's the expected behavior. When you send the command in the Terminal without quotes, you're telling Linux that you want to run that command directly in the Terminal's communication thread, so it naturally takes over the thread's stdout as long as the command is executing.

 

understood, thanks.

 

 

system "nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &"

disable plc 1

close

 

ok; I did it, and we moved the problem in another point: I can get the capp1 running, but I got an error now, that I can read from the log file.

The PLC system call cannot detect the correct LD_LIBRARY_PATH.

 

I added my full needed environmental variable LD_LIBRARY_PATH on the /etc/profile file; the correct value of my var is:

/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib

 

If I echo the variable from a shell (echo $LD_LIBRARY_PATH), I got the correct value.

If I echo the variable from the IDE terminal (system "echo $LD_LIBRARY_PATH"), I still got the correct value.

But, if I echo the variable from the PLC1 (system "echo $LD_LIBRARY_PATH > /var/log/ADSpath"), my LD_LIBRARY_PATH is empty (like it was before I modded the /etc/profile file).

I try to add on the PLC the instruction to export the variable (system "export LD_LIBRARY_PATH=\"/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib\" "), but it seems it does not like the command, and the echo report again an empty LD_LIBRARY_PATH var.

 

Maybe is the PLC is logged differently from the terminal or the common shell?

 

thanks a lot

gigi

Link to comment
Share on other sites

The PLC system call cannot detect the correct LD_LIBRARY_PATH.

 

I added my full needed environmental variable LD_LIBRARY_PATH on the /etc/profile file; the correct value of my var is:

/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib

 

If I echo the variable from a shell (echo $LD_LIBRARY_PATH), I got the correct value.

If I echo the variable from the IDE terminal (system "echo $LD_LIBRARY_PATH"), I still got the correct value.

But, if I echo the variable from the PLC1 (system "echo $LD_LIBRARY_PATH > /var/log/ADSpath"), my LD_LIBRARY_PATH is empty (like it was before I modded the /etc/profile file).

 

This is expected behaviour: /etc/profile is for login shells only.

 

Assuming that the "system" command is executed by a shell, you should be able to use the syntax "VAR=value command" to do what you want. Note that there is space (not semicolon or anything else) between the variable assignment and the command. So in your case the plc should contain: "LD_LIBRARY_PATH=/path1:/path2:... nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &".

 

If that doesn't work, the other way to do it is: create a small shell script that sets the variable then runs your capp1.out. Then invoke this script from the startup plc.

Link to comment
Share on other sites

The PLC system call cannot detect the correct LD_LIBRARY_PATH.

 

I added my full needed environmental variable LD_LIBRARY_PATH on the /etc/profile file; the correct value of my var is:

/opt/ppmac/libppmac:/opt/ppmac/rtpmac:/usr/local/lib:/usr/local/xenomai-2.5.6/lib

 

If I echo the variable from a shell (echo $LD_LIBRARY_PATH), I got the correct value.

If I echo the variable from the IDE terminal (system "echo $LD_LIBRARY_PATH"), I still got the correct value.

But, if I echo the variable from the PLC1 (system "echo $LD_LIBRARY_PATH > /var/log/ADSpath"), my LD_LIBRARY_PATH is empty (like it was before I modded the /etc/profile file).

 

This is expected behaviour: /etc/profile is for login shells only.

 

Assuming that the "system" command is executed by a shell, you should be able to use the syntax "VAR=value command" to do what you want. Note that there is space (not semicolon or anything else) between the variable assignment and the command. So in your case the plc should contain: "LD_LIBRARY_PATH=/path1:/path2:... nohup /usr/local/bin/ads/capp1.out &> /var/log/ADS.txt &".

 

If that doesn't work, the other way to do it is: create a small shell script that sets the variable then runs your capp1.out. Then invoke this script from the startup plc.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

×
×
  • Create New...