System command question (why nothing results?)

In shell:

jari@cosmo:~$ pidof xclock
1422051

in octave:

octave:8> [status,text]=unix("pidof xclock")
status = 1
text =
octave:13> [status,text]=system("pidof xclock")
status = 1
text = 

Latter case I can not get the answer “1422051”…
Something went wrong, but can’t find out yet.

From the documentation of pidof:
pidof(1) - Linux manual page (man7.org)

EXIT STATUS

0 At least one program was found with the requested name.
1 No program was found with the requested name.

It looks correct return code.
It looks like ‘shell’ case behavior is different than with octave ‘system()’-command.
But I can not see why:

Your example works fine for me when I have xclock running in the background:

>> [s, msg] = system ("pidof xclock")
s = 0
msg = 3508

Are you running Octave from snap or flatpak? If so, is the instance of xclock you are trying to find also ran from the same container?

This is the ‘flatpak’ installation.

octave:6> version
ans = 6.3.0

and

jari@cosmo:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Linuxmint
Description:	Linux Mint 20.2
Release:	20.2
Codename:	uma

and ‘xclock’ is as:

jari@cosmo:~$ which xclock
/usr/bin/xclock

This is not necessary to operate…
If I can find the pid of some process (or if some process exists) with the process name from octave by any other means that could be fine also.
I try convert the shell script to octave:

#!/bin/sh
while [ ! -z `pidof -z xclock` ]; do
  echo "looping..."
  sleep 5
done;
echo "done!"

It should become maybe as:

while (1),
  [s, msg]=system("pidof xclock");
  if (length(msg)==0), break; end;
  printf("looping...\n");
  pause(5);
end;

I think that pidof will only find processes that I ran from flatpak, i.e, those that are displayed when you run system ("top") at your octave prompt.

See also Flatpak’s own documentation:
Sandbox Permissions — Flatpak documentation

One of Flatpak’s main goals is to increase the security of desktop systems by isolating applications from one another. This is achieved using sandboxing and means that, by default, applications that are run with Flatpak have extremely limited access to the host environment. This includes:

  • […]
  • No access to processes outside the sandbox.

Also the linuxmint normal pidof gives no results:

octave:3> [s,msg]=system("/usr/bin/pidof xclock")
s = 1
msg =

I couldn’t see easily why this happens and how to fix the situation.
I hope to get the program working…
Or maybe if octave has the pidof() command implemented itself that can tell about this.
Maybe need to downgrade to 5.3.0?
I can not put all the software in the sandbox and it should be get working with this.
I could not see any method to install 6.3.0 octave without the flatpak…
The snap-installation failed to work.

jari@cosmo:~$ snap install octave
octave 6.3.0 from GNU Octave (octave-snap✓) installed
jari@cosmo:~$ octave
bash: /usr/bin/octave: No such file or directory
jari@cosmo:~$ which octave
/snap/bin/octave
jari@cosmo:~$ octave
bash: /usr/bin/octave: No such file or directory
jari@cosmo:~$

maybe $PATH settings issue…
after restart the terminal it works now, but it can not open the file:

octave:1> cknohup
Checking nohup vs. time
error: fprintf: invalid stream number = -1
error: called from
    cknohup at line 6 column 1

in earlier flatpak installation case this worked well…

fn="/home/jari/bin/nohup.txt";
fid=fopen(fn,"w");
fprintf(fid,"Checking nohup vs. time\n");

which ever installation method used some part of the code fails to work.
Snap case the messages for the ‘pidof’ are:

octave:1> cknohup
Checking nohup vs. time
sh: 1: pidof: Permission denied
octave:2> cknohup
Checking nohup vs. time
sh: 1: /usr/bin/pidof: not found

It looks I should rather use the older 5.2.0 octave:

octave:1> system("pidof xclock")
1424882
ans = 0

It looks solution is downgrade the program, but not the solution I was looking for.
So I get it all working only with 5.2.0… also including some commands like:

[s,msg]=system("cat /sys/devices/system/cpu/possible");
ncore=sscanf(msg,"%d-%d")(2)+1;

I’m not using Flatpak myself. So, this might be wrong. But, IIUC, you might be able to use flatpak-spawn --host to spawn a process outside the sandbox:
Ubuntu Manpage: flatpak-spawn - Run commands in a sandbox

I’ll check about it.

something else issues with the “fprintf()”.
It looks like it doesn’t update the output file after each line is written
and delays the output.
Maybe buffering the writing process.

Please, try to stay on topic.
Open a new thread for a different topic.

tks.
I’ll transfer it to a new topic.

Options:

  --built-in-docstrings-file FILE Use docs for built-ins from FILE.
  --debug, -d             Enter parser debugging mode.
  --debug-jit             Enable JIT compiler debugging/tracing.
  --doc-cache-file FILE   Use doc cache file FILE.
  --echo-commands, -x     Echo commands as they are executed.
  --eval CODE             Evaluate CODE.  Exit when done unless --persist.
  --exec-path PATH        Set path for executing subprograms.
  --gui                   Start the graphical user interface.
  --help, -h,             Print short help message and exit.
  --image-path PATH       Add PATH to head of image search path.
  --info-file FILE        Use top-level info file FILE.
  --info-program PROGRAM  Use PROGRAM for reading info files.
  --interactive, -i       Force interactive behavior.
  --jit-compiler          Enable the JIT compiler.
  --line-editing          Force readline use for command-line editing.
  --no-gui                Disable the graphical user interface.
  --no-history, -H        Don't save commands to the history list
  --no-init-file          Don't read the ~/.octaverc or .octaverc files.
  --no-init-path          Don't initialize function search path.
  --no-line-editing       Don't use readline for command-line editing.
  --no-site-file          Don't read the site-wide octaverc file.
  --no-window-system, -W  Disable window system, including graphics.
  --norc, -f              Don't read any initialization files.
  --path PATH, -p PATH    Add PATH to head of function search path.
  --persist               Go interactive after --eval or reading from FILE.
  --silent, --quiet, -q   Don't print message at startup.
  --texi-macros-file FILE Use Texinfo macros in FILE for makeinfo command.
  --traditional           Set variables for closer MATLAB compatibility.
  --verbose, -V           Enable verbose output in some cases.
  --version, -v           Print version number and exit.

  FILE                    Execute commands from FILE.  Exit when done
                          unless --persist is also specified.

I could not find that option. Hopefully this option in octave startup arguments, but not.

Which option are you looking for?

jari@cosmo:~/bin$ flatpak --help
Usage:
  flatpak [OPTION…] COMMAND

Builtin Commands:
 Manage installed applications and runtimes
  install                Install an application or runtime
  update                 Update an installed application or runtime
  uninstall              Uninstall an installed application or runtime
  mask                   Mask out updates and automatic installation
  list                   List installed apps and/or runtimes
  info                   Show info for installed app or runtime
  history                Show history
  config                 Configure flatpak
  repair                 Repair flatpak installation
  create-usb             Put applications or runtimes onto removable media

 Finding applications and runtimes
  search                 Search for remote apps/runtimes

 Running applications
  run                    Run an application
  override               Override permissions for an application
  make-current           Specify default version to run
  enter                  Enter the namespace of a running application
  ps                     Enumerate running applications
  kill                   Stop a running application

 Manage file access
  documents              List exported files
  document-export        Grant an application access to a specific file
  document-unexport      Revoke access to a specific file
  document-info          Show information about a specific file

 Manage dynamic permissions
  permissions            List permissions
  permission-remove      Remove item from permission store
  permission-set         Set permissions
  permission-show        Show app permissions
  permission-reset       Reset app permissions

 Manage remote repositories
  remotes                List all configured remotes
  remote-add             Add a new remote repository (by URL)
  remote-modify          Modify properties of a configured remote
  remote-delete          Delete a configured remote
  remote-ls              List contents of a configured remote
  remote-info            Show information about a remote app or runtime

 Build applications
  build-init             Initialize a directory for building
  build                  Run a build command inside the build dir
  build-finish           Finish a build dir for export
  build-export           Export a build dir to a repository
  build-bundle           Create a bundle file from a ref in a local repository
  build-import-bundle    Import a bundle file
  build-sign             Sign an application or runtime
  build-update-repo      Update the summary file in a repository
  build-commit-from      Create new commit based on existing ref
  repo                   Show information about a repo

Help Options:
  -h, --help             Show help options

Application Options:
  --version              Print version information and exit
  --default-arch         Print default arch and exit
  --supported-arches     Print supported arches and exit
  --gl-drivers           Print active gl drivers and exit
  --installations        Print paths for system installations and exit
  -v, --verbose          Show debug information, -vv for more detail
  --ostree-verbose       Show OSTree debug information

Not sure where it is. I tried as below:

flatpak run org.octave.Octave --persist

Did you even try to read the documentation for the command I proposed earlier?
I even relieved you of using a search engine by including a link to the documentation…
It is called flatpak-spawn! Not flatpak

It looks like I should install it first (as I have only the flatpak installed, no flatpak-spawn).
I can not find flatpak-spawn no matter what.

jari@cosmo:~/bin$ sudo apt search flatpak-spawn
jari@cosmo:~/bin$ sudo apt search flatpak
i   flatpak                                                   - Application deployment framework for desktop apps                   
p   flatpak-builder                                           - Flatpak application building helper                                 
p   flatpak-builder-tests                                     - Application deployment framework for desktop apps (tests)           
p   flatpak-tests                                             - Application deployment framework for desktop apps (tests)           
p   flatpak-xdg-utils                                         - xdg-open and xdg-email reimplementation for containerized apps      
p   flatpak-xdg-utils-tests                                   - as-installed tests for flatpak-xdg-utils                            
i   gir1.2-flatpak-1.0                                        - Application deployment framework for desktop apps (introspection)   
p   gnome-software-plugin-flatpak                             - Flatpak support for GNOME Software                                  
p   libflatpak-dev                                            - Application deployment framework for desktop apps (development)     
p   libflatpak-doc                                            - Application deployment framework for desktop apps (documentation)   
i   libflatpak0                                               - Application deployment framework for desktop apps (library)         
p   plasma-discover-backend-flatpak                           - Discover software management suite - Flatpak backend                
p   plasma-discover-flatpak-backend                           - Discover Flatpak backend - transitional package                     
p   qt5-flatpak-platformtheme                                 - Qt 5 Flatpak platform theme (transitional package)                  
p   qt5-flatpak-platformtheme:i386                            - Qt 5 Flatpak platform theme (transitional package)

can not find…

jari@cosmo:~/bin$ flatpak search flatpak-spawn
No matches found

This becomes very difficult.
I’m guessing the ‘linuxmint’ has disabled the flatpak-spawn options for the security reasons.

Maybe I’ll just change the linux version and test with something like “ZorinOS”, etc.

First sentence of the description:

   Unlike other flatpak commands, flatpak-spawn is available to applications inside the sandbox.

I can not find it inside the sandbox. I guess it must be there, but I can not find it.
I’m not sure how this ‘sandbox’ behaves… but it looks something that is hard to see.
I told you, I can not find it. I guess I need look for it again.
If this is correct example from the manual:


But response is:

jari@cosmo:~$ flatpak-spawn ls /var/run
flatpak-spawn: command not found

I think I can not fully understand what this means, cause I can not see this sandbox
and how it works, etc.
It might work, but I can not see yet how.
There is not further hints why command is not found and how to make it found, but that’s what it says.

When you start Octave with flatpak, it is running inside a sandbox.
In Octave, try running system('flatpak-spawn --host ls ~'). Does that list the files in your home directory?

This seems to work out…
But there is something different that I could not run it from the shell.
Or maybe the manual is hard to be understood.
Finally:

octave:8> [s,msg]=system('flatpak-spawn --host pidof xclock')
s = 0
msg = 23332