Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux daemon     - The security configuration of Linux (Linux)

- Oracle 11g RAC manually playing GI PSU patch ( (Database)

- Java in several ways of using MongoDB (Programming)

- Mount and unloading disks under Linux (Linux)

- Ubuntu UFW firewall settings Introduction (Linux)

- C ++ casts New Standard Comments (Programming)

- Oracle 11g statistics collection - collection of multi-column statistics (Database)

- Security Features Linux and Unix operating system, programming (Linux)

- The relationship between UNIX and Linux (Linux)

- CentOS How to mount the hard drive (Linux)

- Shuffle Process Arrangement in MapReduce (Server)

- Linux Shell Scripting Interview Question (Linux)

- XP virtual machine under VirtualBox solve occupy 100% CPU problem (Linux)

- Android to determine whether the device to open WIFI, GPRS data connection (Programming)

- Python class of operator overloading (Programming)

- Configuration based on open source Lucene Java development environment (Server)

- Linux usage in echo (Linux)

- How to recover deleted files in Linux systems (Linux)

- To install and deploy Apache under the CentOS (Server)

- Source code to compile and install MySQL 5.7.9 (Database)

  Linux daemon
  Add Date : 2018-11-21      
  Linux daemon

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < unistd.h>
#include < fcntl.h>
#include < sys / types.h>
#include < sys / wait.h>

#define MAXFILE 65535

volatile sig_atomic_t _running = 1;

void sigterm_handler (int arg)
    _running = 0;

int main ()
    pid_t pc, pid;
    int i, fd, len, flag = 1;
    char * buf = "this is a Dameon \ n";
    len = strlen (buf);
    // First, create a child process to end the parent process, so the init process adoptions
    pc = fork ();
    if (pc <0) {
        printf ( "error fork \ n");
        exit (1);
    } Else if (pc> 0) {
        exit (0);
    // Next, create a new session, setsid (set session id), get rid of the original control session, to get rid of the original process control group, out of control of the primary control terminal
    pid = setsid ();
    if (pid <0)
        perror ( "setsid error");
    // The third step is to change the working directory
    chdir ( "/");
    // The fourth step, reset the file permission mask
    umask (0);
    // The fifth step, close the file descriptor
    for (i = 0; i         close (i);
    signal (SIGTERM, sigterm_handler);
    while (_running)
        if (flag == 1 && (fd = open ( "/ tmp / daemon.log", O_CREAT | O_WRONLY | O_APPEND, 0600)) <0)
            perror ( "open");
            flag = 0;
            exit (1);
        write (fd, buf, len);
        close (fd);
        usleep (10 * 1000); // 10 ms

Daemon running in the background, without terminals affected.

Each process from the terminal to run, are attached to the terminal. This terminal becomes the controlling terminal of these processes. When the control terminal is closed, the corresponding process will follow automatically shut down.
- See Shell Script Linux Server network traffic (Server)
- Ubuntu use three methods to install Ruby (Linux)
- AngularJS notes --- Scope and controller (Programming)
- Linux Log File Browser --logrotate (Linux)
- Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)
- SSH does not require a password to log on to a Linux server (Server)
- Fedora 23 How to install LAMP server (Server)
- Java in the inverter and covariance (Programming)
- No password on Oracle and MySQL login (Database)
- Formatted output printf command (Programming)
- GAMIT10.5 install and update failed Solution (Linux)
- CentOS 6.4 OpenNebula installation (Linux)
- Comparison of one-time transaction and CTE insert data (Database)
- How to use the Docker Machine cluster deployment Swarm (Server)
- Ubuntu method for single-card dual-IP (Linux)
- Linux / Raspberry Pi using the following command-line based web browser (Linux)
- Iptables principle (Linux)
- Those things packaged using Gradle to Android (Programming)
- Use XtraBackup to MySQL database online incremental backup and recovery (Database)
- Ubuntu prevent arp attacks (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.