How to send ctrl-c to a unix-process started as a port

Nico Weling nico.weling@REDACTED
Fri Oct 19 10:24:22 CEST 2001


Hi All,

with the following programm I'd like to start a UNIX tail. If a line contains the word 'root' I'd like to stop the UNIX-tail process by
sending ctrl-C to the port.

But the tail process didn't stop. Does anybody know why?

Thanks, Nico.

================================================================================

At the Erlang-Shell I entered:

35> gmc_util:tail("tail -f -n0 /var/log/messages").
Bytes: "Oct 19 09:37:01 aims-pc-6 su(pam_unix)[29326]: session opened for user aims by aims(uid=0)\n"
Bytes: "Oct 19 09:37:05 aims-pc-6 su(pam_unix)[29347]: session opened for user root by aims(uid=500)\n"
done

================================================================================

>From the unix-commandline I did:

[root@REDACTED /root]# su aims
[aims@REDACTED /root]$ su root
Password: 
[root@REDACTED /root]# ps -auxww | grep tail
root     29325  0.1  0.4  1548  556 ?        S    09:36   0:00 tail -f -n0 /var/log/messages
root     29381  0.0  0.4  1620  608 pts/7    R    09:37   0:00 grep tail


================================================================================
tail(Cmd) ->
    Command = binary_to_list(
                list_to_binary(["sh -c '", Cmd,
                                "' 2>&0"])),
    Port = open_port({spawn, Command}, [stream,use_stdio,eof]),
    get_data_tail(Port, []).

get_data_tail(Port, Sofar) ->
    receive
        {Port, {data, Bytes}} ->
            io:format("Bytes: ~p~n",[Bytes]),
            case find_string(Bytes,"root") of
                {ok, _} -> 
                    Port ! {self(), {command, "^C"}},
                    done;
                _ ->
                    get_data_tail(Port, Sofar ++ Bytes)
            end;

        {Port, eof} ->
            Port ! {self(), close},
            receive
                {Port, closed} ->
                    true
            end,
            receive
                {'EXIT', Port, _} ->
                    ok
            after 1 ->
                    ok
            end,
            Sofar;

        {'EXIT', Port, _} ->
            Sofar

    after 300000 -> %wait 5mins before exit
            exit(timeout)
    end.



More information about the erlang-questions mailing list