What is the difference between calling a script from a shell and using system ()?

I created a bash script to run some processes on my system. It simply invokes the process and its associated configuration file. Same as command line.

#!/bin/bash # Start specified process in a new session setsid $1 &>/dev/null & 

So, to start someprocess , I would call from the command line:

 root@supercomputer :~# start someprocess 

It works like a charm. Every process, every time. But when I make a system call from another running C ++ process, someprocess never starts.

 system( "start someprocess" ) 

This approach is for 90% of my processes, with the exception of one. The only difference between working and non-working processes is that the non-working one uses its own library routines. I recently added the setsid parameter to the bash script in the hope that starting a new session will help, but that doesn't make any difference. I also tried popen and execv . Without changes.

So my question is what is the difference between calling something with system() and just making the same call from the command line?

All processes are written in C ++ on Linux.

+4
source share
2 answers

.bashrc is only called if bash starts as an interactive shell without logging in. If it is called as a non-interactive shell, as when using system() on a script with shebang bash, it only reads the configuration file pointed to by $BASH_ENV .

This means that you have the following options:

  • add -l to shebang - forces the shell to read ~/.profile at startup
  • set $BASH_ENV to the script you want to get before calling system()
  • add -i to shebang - calls bash as an interactive shell and makes it read ~/.bashrc , but also affects how bash handles input / output.

I would recommend the first option.

You can find a detailed explanation of how bash reads its boot files here . I’m not sure that this will completely solve your problem, but it might seem that in this matter shed light on this part of the problem.

+2
source

Check the environment variables that are used in the system () call. For example, a call system to print some variables and see if they match what you see from the command line.

Most likely, they are not received correctly.

+2
source

Source: https://habr.com/ru/post/1491271/


All Articles