Redirecting stdin through a FIFO - linux

I'm running a server app (written in Java) under GNU/Linux which takes input (from stdin, I guess) and interprets it to run some commands. I dont want to run the app inside a terminal window (I'd like to run a daemon), but I'd still like to be able to input commands whenever I want to. I thought I might be able to do that using fifos, so I created it using mknod. The problem is cat fifofile > java... and cat fifofile | java ... fail with a "file not found" error for some reason.
Using only cat to read and write and the fifo works flawlessly.
Is there any way to fix this, or any other way to achieve the same goal?

So, Minecraft? The best way to do this is to have a bona-fide tty for the console part of the application. screen is an easy way to do that.

Have you tried java < fifofile? What about something like exec 3<&0; exec 0<fifofile; java?
What shell are you using? You might be able to use process substitution or coprocesses if you're using a shell that supports them.

Related

how to provide the password automatically in lua script while sshing on the remote machine

Without using sshpass, sshkeys is there any way to do ssh to a remote machine and provide the password through the lua script only.And also to run a shell command in the background on remote machine after sshing.
How should i take password automatically after executing os.execute('ssh user#192.168.14.81')
That function is not supposed to be used like that. os.execute and io.popen are not really meant to be used at all, actually. They're barebones commands for when you need to do things that are not supposed to work this way but you really need to.
They're both based on standard C library and don't have much to offer. And ssh is handling its interaction with users in some not-so-standard way for security reasons. In general you'll have to use non-standard libraries, like luaposix, or attach some C++ libs for system interaction.
In relation to ssh, there is python library parallel-ssh. Older versions did their thing through ugly parsing of outputs, newer seem to reimplement the whole protocol. With enough desire, it'd be possible to make use of those with lua. Or just use python for the task.
Ass for peculiarities of lua interaction with processes, you may try this code (put it in a file as it'll clog stdin if you try pasting it in terminal)
f=io.popen([[
echo "this will show only on f:read";
echo "now here's your line";
read var; echo $var
]]);
print('this will print before bash command finishes, now type something');
f:write('This will be ignored completely')
print(f:read('*a'));
print('This will print after the bash command');
f:close()

Bash Console commands (Codeship console): how to exit from current "inputs"

Excuse me for the imprecisions in the question but I don't know how it is called what I'm trying.
In the CodeShip documentation is stated that I can pass to the SSH CodeShip debug build some commands using their command line application.
So, I should do something like cs setup-commands and I'm prompted with this:
rof#railsonfire_unique_string_sfivbe8bwucb9:~$ cs setup-commands
Your setup commands:
phpenv local 5.6
phpenv local 5.6
In Your setup commands: I put my commands but then, how can I "execute" them?
The second phpenv local 5.6 line is wrote by the command-line application. I think is something to signal the command were taken, but the behavior is ever the same: I remain "blocked" in the command setup-commands. After setting setup-commands I have to set also test-commands but all the things I write are taken by Your setup commands:.
How can I "submit and exit" the command setup-commands to then launch test-commands and set those other commands?
I think this is something related to Bash, but I don't know what it is...
And I don't know which is the correct terminology.
Can someone help me with this? So I will can also update my question to be more precise. Thank you.
Not sure about your case, but usually the input is considered finished, when th input file (in your case stdin) is closed.
Try to press Ctrl+D, it should end your input (and so signal the program hat you stopped typing for this session)

Launch interactive shell programmatically

I need to do a interactive shell programmatically (in Node.js, but any language will do).
The most important is that I want to start a REPL of any language (node, ruby, erlang etc.) and be able to get autocompletion
pri\t
which I want to get output in stderr (for instance)
print() println() printf()
And because it's in language REPL compgen cannot be used.
I've tried many ways but it seems that it fails because shell can't be interactive while opened programmatically.
Launchin /bin/sh -i just yeilds errors like:
bash: cannot set terminal process group (XXXX): Inappropriate ioctl for device
I've heard that I can't start interactive shell without the terminal, but when doing SSH autocompletion does work which means it can be emulated in some way.
I'd appreciate Your help. I can't continue my work without finding a solution to this.
You can emulate a terminal using a pseudoterminal. This is how ssh does it. There's a lot to using a pseudoterminal though and the SO answer box isn't big enough for all of it. So check the man page for pty(7).

How to run linux application in background which uses shell?

I have an application/binary from a C program which by defaults uses the shell to take inputs from the user. So, when I start the application in background using & it stops automatically, because of the implementation on which I have no access. When I run this code
iStatus = system("./flute-static -send -a232.0.0.1/6666 a.txt &");
It gives output [1] 21970, the pid.
Then if I press another enter, it gives output
[1]+ Stopped ./flute-static -send -a232.0.0.1/6666 a.txt
And obviously it fails to send the data. How can I solve the problem. Please help me. Thanks in advance.
You can try nohup
iStatus = system("nohup ./flute-static -send -a232.0.0.1/6666 a.txt &");
Nohup means: do not terminate this process even when the stty is cut off.
Or You can use screen
https://www.mattcutts.com/blog/a-quick-tutorial-on-screen/
Read Advanced Linux Programming and about the fork system call; you surely want to use fork(2), execve(2), waitpid(2) with some other syscalls(2) and/or perhaps daemon(3) and/or popen(3). Perhaps using strace(1) on the flute-static program might help you understand more of it.
BTW, you might use some FLUTE library (compile MAD-ALCLIB from its source code!), or simply use an HTTP & FTP client library like libcurl
Whatever you do, if a backgrounded process is reading stdin, it is stopped (see signal(7), tty(4) etc...)! Read also the tty demystified

Getting linux terminal value from my application

I am developing a Qt application in Linux. I wanted to pass Linux commands to a terminal. That worked but now i also want to get a response from the terminal for this specific command.
For example,
ls -a
As you know this command lists the directories and files of the current working directory. I now want to pass the returned values from the ls call to my application. What is a correct way to do this?
QProcess is the qt class that will let you spawn a process and read the result. There's an example of usage for reading the result of a command on that page.
popen() , api of linux systerm , return FILE * that you can read it like a file descriptor, may help youp erhaps。
Parsing ls(1) output is dangerous -- make a few files with funny names in a directory and test it out:
touch "one file"
touch "`printf "\x0a\x0a\x0ahello\x0a world"`"
That creates two files in the current working directory. I expect your attempts to parse ls(1) output won't work. This might be alright if you're showing the results to a human, (though a human will be immensely confused if a filename includes output that looks just like ls(1) output!) but if you're trying to present something like an explorer.exe or Finder.app representation of files in the filesystem, this is horribly broken.
Instead, use opendir(3), readdir(3), and closedir(3) to read directory entries yourself. This will be safer, more portable, and (as a side benefit) slightly better performing.

Resources