How can I add a logfile line to the following bash file?


if ps x |grep -v grep |grep -c cccam_2.3.2 >/dev/null
    echo "cccam... ok"
    echo "cccam... restarting"
    /var/bin/cccam_2.3.2 &
  • What do you mean by add a logfile line? Do you want to send the echo stuff to your logfile instead to stdout?
    – Thomas
    Jan 27, 2019 at 14:00
  • I mean:see a log file e.g in tmp or else only" if the program has been restarted"
    – ddinc
    Jan 27, 2019 at 14:19
  • Sorry, still did not get what you actually want to achieve. Do you just want to create an empty file in /tmp? Or do you want to put some information there?
    – Thomas
    Jan 27, 2019 at 14:40

2 Answers 2


I believe you want to use logger command like here:

root@dst:~# logger "Hello log stuff."
root@dst:~# grep Hello /var/log/messages 
Jan 27 16:32:42 dst root: Hello log stuff.

man logger provides nice options too.

  • @Thomas.Should be a file in tmp with only the date and time when the process was restarted
    – ddinc
    Jan 27, 2019 at 17:05
  • @ddinc logger uses the standard logging facility - an excellent suggestion given you only said you wanted a log line, and didn't specify it had to be a suboptimal home-grown solution. Jan 27, 2019 at 17:08
  • sorry for causing misunderstanding.but I still don't know how to put a logger command everytime ""echo "cccam... restarting""" occurs.
    – ddinc
    Jan 28, 2019 at 9:08
  • add line: logger "cccam... restarting" Jan 28, 2019 at 10:04

I think you can also add -x with shell script and redirect that to a logfile. I use it to log and debug few shell scripts

Something like this

sh -x your_script.sh 2> logfile.log
  • don't know if I got it right but both suggestions produce no logs #!/bin/sh if ps x |grep -v grep |grep -c cccam_2.3.2 >/dev/null then echo "cccam... ok" else echo "cccam... restarting" /var/bin/cccam_2.3.2 & sh -x cccam.sh 2> logfile.log fi
    – ddinc
    Jan 29, 2019 at 9:29
  • @ddinc what do you see if you run the sh -x your_script.sh 2> logfile.log outside the script. This should create a log of the shell script while its running into the logfile you mention in the same location.
    – Atul
    Jan 29, 2019 at 15:19
  • get this:root@dm820:/var/bin# sh -x cccam.sh 2> logfile.log cccam... ok root@dm820:/var/bin# cccam.sh cccam... ok root@dm820:/var/bin# no log though
    – ddinc
    Jan 30, 2019 at 8:20
  • [root@localhost ~]# sh -x shell.sh 2> log cccam... restarting [root@localhost ~]# cat log + grep -v grep + ps x + grep -c cccam_2.3.2 + echo 'cccam... restarting' + /var/bin/cccam_2.3.2 shell.sh: line 8: /var/bin/cccam_2.3.2: No such file or directory I did a small test on one my VM using your code and see a log like this. I didnt had the right files so i got error. Is this close to what you need ?
    – Atul
    Jan 30, 2019 at 10:50

You must log in to answer this question.

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