Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
bash running nc with ampersand terminates program

I want to run nc via & and then manually feed data in stdin from /proc filesystem whenever I want. so the problem is:

if I run nc 127.0.0.1 1234 &

program runs in background and I can write in stdin whatever I want. but, if I create test.sh and add

#!/bin/bash
nc 127.0.0.1 1234 &
sleep 20

it connects to 1234 and terminates immediately (doesn't even wait for 20 seconds). why? I was suspecting it gets it's stdin written from somewhere.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If I get your purpose right, you want to feed data manually to nc which will be sent to client.

You can use named pipes for this purpose.

cat /tmp/f | ./parser.sh 2>&1 | nc -lvk 127.0.0.1 1234 > /tmp/f

where /tmp/f is a pipe made using mkfifo /tmp/f

whatever you want to feed to nc can be echo-ed in parser.sh

over 4 years ago · Santiago Trujillo Relatório

0

Interesting question.

The bash manpage states:

   If  a  command  is  followed  by a & and job control is not active, the
   default standard input for the command is  the  empty  file  /dev/null.
   Otherwise,  the  invoked  command  inherits the file descriptors of the
   calling shell as modified by redirections.

If you call nc 127.0.0.1 1234 < /dev/null outside a shell script (with job control) it will result in the same.

You can change your bash script like this to make it work:

#!/bin/bash
nc 127.0.0.1 1234 < /dev/stdin &
sleep 20
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda