3

Can anyone help me? The program's objective is to kill the processes running on port 443 and to then start xampp automatically.

What I've done:

#include <stdio.h>
#include <stdlib.h>

main() {
    system("netstat -tulpn | grep :443");
    /* If 443 is busy (kill all)*/
    /*next -> */ system("/opt/lampp/xampp start");
}
4
  • 1
    You're trying to kill a fly with a sledgehammer, do you really need to use C? Is this for a didactic purpose?
    – kos
    May 5, 2015 at 15:50
  • 2
    I just I used c because I know it's possible, but I accept any solutions :) Its not didactic purpose....
    – Dragon21
    May 5, 2015 at 16:00
  • You want to kill a process? Do you know the name of the process? Or the process ID?
    – Tim
    May 5, 2015 at 16:05
  • 2
    @Dragon21 I recommend you edit your question to clarify explicitly that you do not specifically require a C solution. It appears some people are voting to close this question because they think it's primarily about C programming and not process management. If you edit your question to clarify your actual needs and goals, that may be helpful. May 5, 2015 at 16:23

2 Answers 2

6

To kill a process on a port you can do:

$ fuser -n tcp -k 443 && /opt/lampp/xampp start

Change the && to ; if you want it to start xampp no matter if there was anything running on that port or not.

1
  • +1 for an actual solution without a counterproductive C wrapping around it. May 5, 2015 at 20:05
4

Using C (note that main() usually returns an int):

#include <stdio.h>
#include <stdlib.h>

int main() {
    int ret;

    ret=system("fuser -k 443/tcp; /opt/lampp/xampp start");
    return ret;
}

Or simply using bash:

fuser -k 443/tcp; /opt/lampp/xampp start

Using ; in place of && in either methods makes /opt/lampp/xampp start execute despite fuser -k 443/tcp not killing any process.

11
  • 1
    But If 443 not busy I have that: "sh: 1: kill: No such process" and xampp don't start :(
    – Dragon21
    May 5, 2015 at 16:11
  • 2
    @Dragon21 You asked how to kill process 443, which is the process you're actually trying to kill?
    – kos
    May 5, 2015 at 16:13
  • 443 but if I stop xampp and try start with that script he dont start. Maybe because 443 is not busy now and xampp command not run next.
    – Dragon21
    May 5, 2015 at 16:19
  • 1
    Not the process, the port 433
    – A.B.
    May 5, 2015 at 16:23
  • 1
    Or with awk ;) netstat -tulpn | awk '$4 ~ /\.[0-9]{1,}:443/ && $6 ~ /[0-9]*\// {match($6,"[0-9]+",a)} END { print a[0] }'
    – A.B.
    May 5, 2015 at 16:41

Not the answer you're looking for? Browse other questions tagged .